[PyQt] how to set accept-language

Peter Irbizon peterirbizon at gmail.com
Thu May 22 10:31:11 BST 2014


Hi
I have this browser code and I am trying to put accept-language header. I
have no idea how to do it in this code. Can you help me what/where to add
it? Many thanks

#!/usr/bin/env python

from PyQt4 import QtCore, QtGui, QtNetwork, QtWebKit
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *

from BeautifulSoup import BeautifulSoup

try:
    import jquery_rc3
except ImportError:
    import jquery_rc2



class FakeBrowser(QWebPage):
    def __init__(self, parent=None, **kwargs):
        QWebPage.__init__(self, parent, **kwargs)

    def setUserAgent(self, agent):
        self.useragent = agent

    def userAgentForUrl(self, url): return self.useragent

class MainWindow(QtGui.QMainWindow):
    def __init__(self, url):
        super(MainWindow, self).__init__()

        self.progress = 0

        MyApp="oooa"
        """
           window.navigator = {
          appCodeName: '"""+MyApp+"""MyApp',
          appName: 'aMyNewApp',
          vendor: 'MyCompany'
        }
        """
        getJsValue = """
        """
        self.jQuery = getJsValue
        print self.jQuery

        QtNetwork.QNetworkProxyFactory.setUseSystemConfiguration(True)

        self._webPage=FakeBrowser(self)
        self._webPage.setUserAgent("Mozilla/5.0 (Windows NT 5.1)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36")

        self.view = QtWebKit.QWebView(self)

        self.view.setPage(self._webPage)

        self.view.load(url)


self.view.page().mainFrame().javaScriptWindowObjectCleared.connect(self.pokusnejs)
        self.view.loadProgress.connect(self.setProgress)
        self.view.loadFinished.connect(self.adjustLocation)
        self.view.titleChanged.connect(self.adjustTitle)
        self.view.loadFinished.connect(self.finishLoading)

        self.locationEdit = QtGui.QLineEdit(self)
        self.locationEdit.setSizePolicy(QtGui.QSizePolicy.Expanding,
                self.locationEdit.sizePolicy().verticalPolicy())
        self.locationEdit.returnPressed.connect(self.changeLocation)

        toolBar = self.addToolBar("Navigation")
        toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Back))
        toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Forward))
        toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Reload))
        toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Stop))
        toolBar.addWidget(self.locationEdit)

        viewMenu = self.menuBar().addMenu("&View")
        viewSourceAction = QtGui.QAction("Page Source", self)
        viewSourceAction.triggered.connect(self.fkViewSource)
        viewMenu.addAction(viewSourceAction)

        effectMenu = self.menuBar().addMenu("&Effect")
        effectMenu.addAction("Highlight all links", self.highlightAllLinks)

        self.rotateAction = QtGui.QAction(
                self.style().standardIcon(
                        QtGui.QStyle.SP_FileDialogDetailedView),
                "Turn images upside down", self, checkable=True,
                toggled=self.rotateImages)
        effectMenu.addAction(self.rotateAction)

        toolsMenu = self.menuBar().addMenu("&Tools")
        toolsMenu.addAction("Remove GIF images", self.removeGifImages)
        toolsMenu.addAction("Remove all inline frames",
                self.removeInlineFrames)
        toolsMenu.addAction("Remove all object elements",
                self.removeObjectElements)
        toolsMenu.addAction("Remove all embedded elements",
                self.removeEmbeddedElements)
        self.setCentralWidget(self.view)
        self.setUnifiedTitleAndToolBarOnMac(True)

        self.view.loadFinished.connect(self.fkSource)

    def fkSource(self):
        pass

    def fkViewSource(self):
        self.textEdit = QtGui.QTextEdit(None)
        self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.textEdit.show()
        self.textEdit.setPlainText(self.HTML)
        self.textEdit.resize(600, 400)

    def viewSource(self):
        accessManager = self.view.page().networkAccessManager()
        request = QtNetwork.QNetworkRequest(self.view.url())
        reply = accessManager.get(request)
        reply.finished.connect(self.slotSourceDownloaded)

    def slotSourceDownloaded(self):
        reply = self.sender()
        self.textEdit = QtGui.QTextEdit(None)
        self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.textEdit.show()
        self.textEdit.setPlainText(QtCore.QTextStream(reply).readAll())
        self.textEdit.resize(600, 400)
        reply.deleteLater()

    def adjustLocation(self):
        self.locationEdit.setText(self.view.url().toString())

    def changeLocation(self):
        url = QtCore.QUrl.fromUserInput(self.locationEdit.text())
        self.view.load(url)
        self.view.setFocus()

    def adjustTitle(self):
        if 0 < self.progress < 100:
            self.setWindowTitle("%s (%s%%)" % (self.view.title(),
self.progress))
        else:
            self.setWindowTitle(self.view.title())

    def pokusnejs(self):
        self.view.page().mainFrame().evaluateJavaScript(self.jQuery)

    def setProgress(self, p):
        self.view.page().mainFrame().evaluateJavaScript(self.jQuery)

    def finishLoading(self):
        self.progress = 100
        self.adjustTitle()
        self.rotateImages(self.rotateAction.isChecked())

    def highlightAllLinks(self):
        code = """$('a').each(
                    function () {
                        $(this).css('background-color', 'yellow')
                    }
                  )"""
        self.view.page().mainFrame().evaluateJavaScript(code)

    def rotateImages(self, invert):
        if invert:
            code = """
                $('img').each(
                    function () {
                        $(this).css('-webkit-transition',
'-webkit-transform 2s');
                        $(this).css('-webkit-transform', 'rotate(180deg)')
                    }
                )"""
        else:
            code = """
                $('img').each(
                    function () {
                        $(this).css('-webkit-transition',
'-webkit-transform 2s');
                        $(this).css('-webkit-transform', 'rotate(0deg)')
                    }
                )"""

        self.view.page().mainFrame().evaluateJavaScript(code)

    def removeGifImages(self):
        code = "$('[src*=gif]').remove()"
        self.view.page().mainFrame().evaluateJavaScript(code)

    def removeInlineFrames(self):
        code = "$('iframe').remove()"
        self.view.page().mainFrame().evaluateJavaScript(code)

    def removeObjectElements(self):
        code = "$('object').remove()"
        self.view.page().mainFrame().evaluateJavaScript(code)

    def removeEmbeddedElements(self):
        code = "$('embed').remove()"
        self.view.page().mainFrame().evaluateJavaScript(code)


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)

    if len(sys.argv) > 1:
        url = QtCore.QUrl(sys.argv[1])
    else:
        url = QtCore.QUrl('http://domain.com')

    browser = MainWindow(url)
    browser.show()

    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140522/d88cb150/attachment-0001.html>


More information about the PyQt mailing list