|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on 2022/05/12 |
| 5 | +@author: Irony |
| 6 | +@site: https://pyqt.site https://github.com/PyQt5 |
| 7 | +@email: 892768447@qq.com |
| 8 | +@file: SetCookies.py |
| 9 | +@description: 主动设置Cookie |
| 10 | +""" |
| 11 | + |
| 12 | +try: |
| 13 | + from PyQt5.QtCore import QDateTime, Qt, QUrl |
| 14 | + from PyQt5.QtWebEngineWidgets import QWebEngineProfile, QWebEngineView |
| 15 | + from PyQt5.QtWidgets import QApplication |
| 16 | + from PyQt5.QtNetwork import QNetworkCookie |
| 17 | +except ImportError: |
| 18 | + from PySide2.QtCore import QDateTime, Qt, QUrl |
| 19 | + from PySide2.QtWebEngineWidgets import QWebEngineProfile, QWebEngineView |
| 20 | + from PySide2.QtWidgets import QApplication |
| 21 | + from PySide2.QtNetwork import QNetworkCookie |
| 22 | + |
| 23 | +cookies = [{ |
| 24 | + "domain": "pyqt.site", |
| 25 | + "expirationDate": 1714906174.734258, |
| 26 | + "hostOnly": True, |
| 27 | + "httpOnly": False, |
| 28 | + "name": "snexid", |
| 29 | + "path": "/", |
| 30 | + "sameSite": None, |
| 31 | + "secure": False, |
| 32 | + "session": False, |
| 33 | + "storeId": None, |
| 34 | + "value": "1-22-333-4444-55555" |
| 35 | +}, { |
| 36 | + "domain": "pyqt.site", |
| 37 | + "expirationDate": 1714906174.734258, |
| 38 | + "hostOnly": True, |
| 39 | + "httpOnly": True, |
| 40 | + "name": "testonly", |
| 41 | + "path": "/", |
| 42 | + "secure": True, |
| 43 | + "value": "testonly" |
| 44 | +}, { |
| 45 | + "domain": "pyqt.site", |
| 46 | + "hostOnly": True, |
| 47 | + "httpOnly": False, |
| 48 | + "name": "test", |
| 49 | + "path": "/", |
| 50 | + "secure": False, |
| 51 | + "value": "test" |
| 52 | +}] |
| 53 | + |
| 54 | + |
| 55 | +class Window(QWebEngineView): |
| 56 | + |
| 57 | + def __init__(self, *args, **kwargs): |
| 58 | + super(Window, self).__init__(*args, **kwargs) |
| 59 | + # global |
| 60 | + # self.cookieStore = QWebEngineProfile.defaultProfile().cookieStore() |
| 61 | + |
| 62 | + # current |
| 63 | + self.cookieStore = self.page().profile().cookieStore() |
| 64 | + self.initCookies() |
| 65 | + self.loadProgress.connect(self.onLoadProgress) |
| 66 | + self.load(QUrl('https://pyqt.site')) |
| 67 | + |
| 68 | + def onLoadProgress(self, progress): |
| 69 | + if progress == 100: |
| 70 | + # 测试获取cookie |
| 71 | + self.page().runJavaScript('alert(document.cookie);') |
| 72 | + |
| 73 | + def initCookies(self): |
| 74 | + for cookie in cookies: |
| 75 | + qcookie = QNetworkCookie() |
| 76 | + qcookie.setName(cookie.get('name', '').encode()) |
| 77 | + qcookie.setValue(cookie.get('value', '').encode()) |
| 78 | + qcookie.setDomain(cookie.get('domain', '')) |
| 79 | + qcookie.setPath(cookie.get('path', '')) |
| 80 | + qcookie.setExpirationDate( |
| 81 | + QDateTime.fromString(str(cookie.get('expirationDate', 0)), |
| 82 | + Qt.ISODate)) |
| 83 | + qcookie.setHttpOnly(cookie.get('httpOnly', False)) |
| 84 | + qcookie.setSecure(cookie.get('secure', False)) |
| 85 | + # 注意可以设置具体的url |
| 86 | + self.cookieStore.setCookie(qcookie, QUrl()) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + import cgitb |
| 91 | + import sys |
| 92 | + |
| 93 | + cgitb.enable(format='text') |
| 94 | + app = QApplication(sys.argv) |
| 95 | + w = Window() |
| 96 | + w.show() |
| 97 | + sys.exit(app.exec_()) |
0 commit comments