4

我正在尝试存储从网站获得的所有 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_()
4

1 回答 1

1

loadFinished无论如何,您可能应该在检查 cookie 之前等待,也许这样的东西对您有用,在这里我重新实现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_())
于 2013-01-04T04:46:30.000 回答