I'm trying to store all cookies that I get from a website and than print them. Please help me to get it to work, because I think I don't store anything, and that's the reason why I have nothing on the output.
I'm testing this code on google website, and I'm pretty sure they store cookie once you get to there page.
So I'm trying to get all available cookies and print them. here is the code:
#! /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_()