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