gpt4 book ai didi

python - 从 QNetworkCookie、PyQt4 打印 cookie

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:49 24 4
gpt4 key购买 nike

我正在尝试存储从网站获得的所有 cookie,而不是打印它们。请帮助我让它工作,因为我认为我没有存储任何东西,这就是我没有输出任何内容的原因。

我正在谷歌网站上测试这段代码,我很确定一旦你到达该页面,他们就会存储 cookie。

所以我正在尝试获取所有可用的 cookie 并打印它们。这是代码:

#! /usr/bin/env python2.7


from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
from PyQt4.QtWebKit import *
import sys, os, simplejson, signal

class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.tabs = QTabWidget()
self.setCentralWidget(self.tabs)
self.settings = QSettings()
self.numberOfTabs = 0
self.cookies = QNetworkCookieJar()
self.cookies.setAllCookies([QNetworkCookie.parseCookies(c)[0] for c in self.get('cookiejar')])

def put(self, key, value):
self.settings.setValue(key, simplejson.dumps(value))
self.settings.sync()

def get(self, key):
v = self.settings.value(key)
return simplejson.loads(unicode(v.toString()))

def addNewTab(self, url=QUrl('')):
self.numberOfTabs += 1
self.tabs.setCurrentIndex(self.tabs.addTab(Tab(QUrl(url)),'%s'%str(self.numberOfTabs)))
self.setCookies()
return self.tabs.currentWidget()

def setCookies(self):
self.put('cookiejar', [str(c.toRawForm()) for c in self.cookies.allCookies()])
print self.get('cookiejar')

class Tab(QWebView):
def __init__(self, url):
QWebView.__init__(self)
self.load(url)

if __name__ == '__main__':
app = QApplication(sys.argv)
browser = MainWindow()
browser.addNewTab('https://google.com')
browser.show()
if signal.signal(signal.SIGINT, signal.SIG_DFL):
sys.exit(app.exec_())
app.exec_()

最佳答案

您可能应该在检查 cookie 之前等待 loadFinished,无论如何,也许这样的事情对您有用,在这里我重新实现了 QNetworkCookieJar 并完成了所有存储和存储的工作从那里加载 cookie:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork

class cookieJar(QtNetwork.QNetworkCookieJar):
def __init__(self, cookiesKey, parent=None):
super(cookieJar, self).__init__(parent)

self.mainWindow = parent
self.cookiesKey = cookiesKey
cookiesValue = self.mainWindow.settings.value(self.cookiesKey)

if cookiesValue:
cookiesList = QtNetwork.QNetworkCookie.parseCookies(cookiesValue)
self.setAllCookies(cookiesList)

def setCookiesFromUrl (self, cookieList, url):
cookiesValue = self.mainWindow.settings.value(self.cookiesKey)
cookiesArray = cookiesValue if cookiesValue else QtCore.QByteArray()

for cookie in cookieList:
cookiesArray.append(cookie.toRawForm() + "\n")

self.mainWindow.settings.setValue(self.cookiesKey, cookiesArray)

return super(cookieJar, self).setCookiesFromUrl(cookieList, url)

class webView(QtWebKit.QWebView):
def __init__(self, cookiesKey, url, parent=None):
super(webView, self).__init__(parent)

self.cookieJar = cookieJar(cookiesKey, parent)

self.page().networkAccessManager().setCookieJar(self.cookieJar)

class myWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)

self.cookiesKey = "cookies"

self.centralwidget = QtGui.QWidget(self)

self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setTabsClosable(True)

self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.addWidget(self.tabWidget)

self.actionTabAdd = QtGui.QAction(self)
self.actionTabAdd.setText("Add Tab")
self.actionTabAdd.triggered.connect(self.on_actionTabAdd_triggered)

self.lineEdit = QtGui.QLineEdit(self)
self.lineEdit.setText("http://www.example.com")

self.toolBar = QtGui.QToolBar(self)
self.toolBar.addAction(self.actionTabAdd)
self.toolBar.addWidget(self.lineEdit)

self.addToolBar(QtCore.Qt.ToolBarArea(QtCore.Qt.TopToolBarArea), self.toolBar)
self.setCentralWidget(self.tabWidget)

self.settings = QtCore.QSettings()

@QtCore.pyqtSlot()
def on_actionShowCookies_triggered(self):
webView = self.tabWidget.currentWidget()
listCookies = webView.page().networkAccessManager().cookieJar().allCookies()

for cookie in listCookies:
print cookie.toRawForm()

@QtCore.pyqtSlot()
def on_actionTabAdd_triggered(self):
url = self.lineEdit.text()
self.addNewTab(url if url else 'about:blank')

def addNewTab(self, url):
tabName = u"Tab {0}".format(str(self.tabWidget.count()))

tabWidget= webView(self.cookiesKey, url, self)
tabWidget.loadFinished.connect(self.on_tabWidget_loadFinished)
tabWidget.load(QtCore.QUrl(url))

tabIndex = self.tabWidget.addTab(tabWidget, tabName)

self.tabWidget.setCurrentIndex(tabIndex)

@QtCore.pyqtSlot()
def on_tabWidget_loadFinished(self):
print self.settings.value(self.cookiesKey)

if __name__ == "__main__":
import sys

app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')

main = myWindow()
main.resize(666, 333)
main.show()

sys.exit(app.exec_())

关于python - 从 QNetworkCookie、PyQt4 打印 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14148263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com