Tom*_*nna 3 python cookies python-3.x pyqt5 qtwebengine
标题确实说明了一切。想知道如何获取 QWebEngineProfile 的 cookie 作为其名称和值的字典或 json 格式。我正在使用 PyQt5。
eyl*_*esc 10
QWebEngineCookieStore是一个管理cookies的类,我们可以通过cookieStore()方法访问这个对象,为了获取cookies可以通过cookieAdded信号异步完成,在下面的部分我们将展示一个例子:
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.webview = QWebEngineView()
profile = QWebEngineProfile("storage", self.webview)
cookie_store = profile.cookieStore()
cookie_store.cookieAdded.connect(self.onCookieAdded)
self.cookies = []
webpage = QWebEnginePage(profile, self.webview)
self.webview.setPage(webpage)
self.webview.load(
QUrl("/sf/ask/3370522501/"))
self.setCentralWidget(self.webview)
def onCookieAdded(self, cookie):
for c in self.cookies:
if c.hasSameIdentifier(cookie):
return
self.cookies.append(QNetworkCookie(cookie))
self.toJson()
def toJson(self):
cookies_list_info = []
for c in self.cookies:
data = {"name": bytearray(c.name()).decode(), "domain": c.domain(), "value": bytearray(c.value()).decode(),
"path": c.path(), "expirationDate": c.expirationDate().toString(Qt.ISODate), "secure": c.isSecure(),
"httponly": c.isHttpOnly()}
cookies_list_info.append(data)
print("Cookie as list of dictionary:")
print(cookies_list_info)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2364 次 |
| 最近记录: |