0

I want to do this :

  1. access "www.example.com" without cookies (I need finished the whole loading, include js etc.)
  2. after load finished, I want to know if there are cookies been written.

If I use pyqt, how can I delete the cookies and get the cookies?

Here's the code i've found familiar to my request(i don't know how to delete cookies when it exit.actually i want to test when i access a web what it write to my cookies.including the the web from js):

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_())
marsen
  • 101
  • 1
  • 8
  • So what is the problem with the code you have? Errors (provide full traceback)? Unexpected outputs (provide inputs and expected and actual outputs)? – jonrsharpe Feb 20 '14 at 08:09
  • Wouldn't be mechanize fit your needs? I'd never checked on cookies there but here is a link --> http://stackoverflow.com/questions/606072/python-how-to-dump-cookies-of-a-mechanize-browser-instance – xhallix Feb 20 '14 at 08:26
  • mechnize doesn't fit my need,it doesnot support js,it can just access the single url. – marsen Feb 20 '14 at 09:16
  • the code i have does not have errors ,but i want it to delete the cookies when it exit. Actually i want to test when i access a web what it write to my cookie.not only the response the url itself also the second or the third url. – marsen Feb 20 '14 at 09:21
  • 1
    Have a look at phantomJS and selenium. – User Feb 20 '14 at 10:05
  • thanks,it works.here is the code: – marsen Feb 21 '14 at 01:50

1 Answers1

0

selenium fit my need perfect,here's the code:

from selenium import webdriver
import time


def main():
    driver = webdriver.Firefox()
    driver.get("http://stackoverflow.com/")
    time.sleep(5)
    cookie= driver.get_cookies()
    print cookie
    driver.delete_all_cookies()
    print '=========================================='
    print driver.get_cookies()

if __name__ == "__main__":
    main()
marsen
  • 101
  • 1
  • 8