网站内链代码,企业策划工作内容,海口手机端建站模板,湛江设计公司目录 专栏导读1、背景介绍2、库的安装3、核心代码4、完整代码总结 专栏导读 #x1f338; 欢迎来到Python办公自动化专栏—Python处理办公问题#xff0c;解放您的双手 #x1f3f3;️#x1f308; 博客主页#xff1a;请点击—— 一晌小贪欢的博客主页求关注 欢迎来到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、核心代码 ①保存Cookie等信息
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, encodingutf-8) as f:json.dump(cookie_dict, f, ensure_asciiFalse, indent4)QMessageBox.information(self, 成功, 保存成功)except Exception as e:QMessageBox.critical(self, 错误, f保存失败{str(e)})
4、完整代码
# -*- coding: UTF-8 -*-Project Cookie收集界面工具
File _0_cookie_.py
IDE PyCharm
Author 一晌小贪欢278865463qq.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;})# 创建accessToken输入框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)# 创建Cookie输入框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)# 加载现有的cookie数据self.load_existing_cookie()def load_existing_cookie(self):try:with open(cookie_json/cookie.json, r, encodingutf-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, encodingutf-8) as f:json.dump(cookie_dict, f, ensure_asciiFalse, indent4)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基础学习专栏