目录
- 专栏导读
- 1、背景介绍
- 2、库的安装
- 3、核心代码
- 4、完整代码
- 总结
专栏导读

-
🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手
-
🏳️🌈 博客主页:请点击——> 一晌小贪欢的博客主页求关注
-
👍 该系列文章专栏:请点击——>Python办公自动化专栏求订阅
-
🕷 此外还有爬虫专栏:请点击——>Python爬虫基础专栏求订阅
-
📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅
-
文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏
-
❤️ 欢迎各位佬关注! ❤️
1、背景介绍
-
有时候我们在爬虫的时候,经常使用到Cookie,于是为了方便复制粘贴,可以将这一部分单独分离出来,只用来粘贴Cookie,然后存入json中


-
其他程序如果需要,就可以直接加载这个Cookie,然后直接使用,也可以直接上线上化,存入某个网络地址中,直接下载使用
2、库的安装
库 | 用途 | 安装 |
---|
PyQt5 | 界面设计 | pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
3、核心代码
def save_cookie(self):cookie_dict = {"accessToken": self.access_token_input.text(),"Cookie": self.cookie_input.text()}if not os.path.exists("cookie_json"):os.makedirs("cookie_json")try:with open("cookie_json/cookie.json", "w", encoding="utf-8") as f:json.dump(cookie_dict, f, ensure_ascii=False, indent=4)QMessageBox.information(self, "成功", "保存成功!")except Exception as e:QMessageBox.critical(self, "错误", f"保存失败:{str(e)}")
4、完整代码
'''
@Project :Cookie收集界面工具
@File :_0_cookie_.py
@IDE :PyCharm
@Author :一晌小贪欢(278865463@qq.com)
@Date :2025/1/23 19:08
'''
import os
import sys
import json
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,QLabel, QLineEdit, QPushButton, QMessageBox)class CookieToolWindow(QMainWindow):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setWindowTitle('Cookie设置工具')self.setGeometry(300, 300, 500, 250) central_widget = QWidget()self.setCentralWidget(central_widget)layout = QVBoxLayout(central_widget)layout.setSpacing(15) self.setStyleSheet("""QMainWindow {background-color: #f5f5f5;}QLabel {font-size: 14px;font-weight: bold;color: #333333;}QLineEdit {padding: 8px;font-size: 13px;border: 1px solid #cccccc;border-radius: 4px;background-color: white;}QPushButton {padding: 10px;font-size: 14px;font-weight: bold;color: white;background-color: #2196F3;border: none;border-radius: 4px;min-height: 40px;}QPushButton:hover {background-color: #1976D2;}""")self.access_token_label = QLabel('Access Token:')self.access_token_input = QLineEdit()self.access_token_input.setPlaceholderText('请输入Access Token')layout.addWidget(self.access_token_label)layout.addWidget(self.access_token_input)self.cookie_label = QLabel('Cookie:')self.cookie_input = QLineEdit()self.cookie_input.setPlaceholderText('请输入Cookie')layout.addWidget(self.cookie_label)layout.addWidget(self.cookie_input)self.save_button = QPushButton('保存')self.save_button.clicked.connect(self.save_cookie)layout.addWidget(self.save_button)layout.setContentsMargins(20, 20, 20, 20)self.load_existing_cookie()def load_existing_cookie(self):try:with open("cookie_json/cookie.json", "r", encoding="utf-8") as f:cookie_dict = json.load(f)self.access_token_input.setText(cookie_dict.get("accessToken", ""))self.cookie_input.setText(cookie_dict.get("Cookie", ""))except FileNotFoundError:passdef save_cookie(self):cookie_dict = {"accessToken": self.access_token_input.text(),"Cookie": self.cookie_input.text()}if not os.path.exists("cookie_json"):os.makedirs("cookie_json")try:with open("cookie_json/cookie.json", "w", encoding="utf-8") as f:json.dump(cookie_dict, f, ensure_ascii=False, indent=4)QMessageBox.information(self, "成功", "保存成功!")except Exception as e:QMessageBox.critical(self, "错误", f"保存失败:{str(e)}")def main():app = QApplication(sys.argv)window = CookieToolWindow()window.show()sys.exit(app.exec_())if __name__ == '__main__':main()
总结
-
希望对初学者有帮助
-
致力于办公自动化的小小程序员一枚
-
希望能得到大家的【一个免费关注】!感谢
-
求个 🤞 关注 🤞
-
此外还有办公自动化专栏,欢迎大家订阅:Python办公自动化专栏
-
求个 ❤️ 喜欢 ❤️
-
此外还有爬虫专栏,欢迎大家订阅:Python爬虫基础专栏
-
求个 👍 收藏 👍
-
此外还有Python基础专栏,欢迎大家订阅:Python基础学习专栏