<div dir="ltr">Hi<div>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</div><div><br></div><div><div>#!/usr/bin/env python</div>
<div><br></div><div>from PyQt4 import QtCore, QtGui, QtNetwork, QtWebKit</div><div>from PyQt4.QtCore import *</div><div>from PyQt4.QtGui import *</div><div>from PyQt4.QtWebKit import *</div><div>from PyQt4.QtNetwork import *</div>
<div><br></div><div>from BeautifulSoup import BeautifulSoup </div><div><br></div><div>try:</div><div> import jquery_rc3</div><div>except ImportError:</div><div> import jquery_rc2</div><div><br></div><div><br></div><div>
<br></div><div>class FakeBrowser(QWebPage):</div><div> def __init__(self, parent=None, **kwargs):</div><div> QWebPage.__init__(self, parent, **kwargs)</div><div><br></div><div> def setUserAgent(self, agent):</div>
<div> self.useragent = agent</div><div><br></div><div> def userAgentForUrl(self, url): return self.useragent</div><div><br></div><div>class MainWindow(QtGui.QMainWindow):</div><div> def __init__(self, url):</div>
<div> super(MainWindow, self).__init__()</div><div><br></div><div> self.progress = 0</div><div> </div><div> MyApp="oooa"</div><div> """</div><div> window.navigator = {</div>
<div> appCodeName: '"""+MyApp+"""MyApp',</div><div> appName: 'aMyNewApp',</div><div> vendor: 'MyCompany'</div><div> }</div><div> """</div>
<div> getJsValue = """ </div><div> """ </div><div> self.jQuery = getJsValue</div><div> print self.jQuery </div><div> </div><div> QtNetwork.QNetworkProxyFactory.setUseSystemConfiguration(True)</div>
<div><br></div><div> self._webPage=FakeBrowser(self)</div><div> self._webPage.setUserAgent("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36")</div>
<div><br></div><div> self.view = QtWebKit.QWebView(self)</div><div> </div><div> self.view.setPage(self._webPage)</div><div> </div><div> self.view.load(url)</div><div><br></div><div> self.view.page().mainFrame().javaScriptWindowObjectCleared.connect(self.pokusnejs) </div>
<div> self.view.loadProgress.connect(self.setProgress) </div><div> self.view.loadFinished.connect(self.adjustLocation)</div><div> self.view.titleChanged.connect(self.adjustTitle)</div><div> self.view.loadFinished.connect(self.finishLoading)</div>
<div><br></div><div> self.locationEdit = QtGui.QLineEdit(self)</div><div> self.locationEdit.setSizePolicy(QtGui.QSizePolicy.Expanding,</div><div> self.locationEdit.sizePolicy().verticalPolicy())</div>
<div> self.locationEdit.returnPressed.connect(self.changeLocation)</div><div><br></div><div> toolBar = self.addToolBar("Navigation")</div><div> toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Back))</div>
<div> toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Forward))</div><div> toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Reload))</div><div> toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Stop))</div>
<div> toolBar.addWidget(self.locationEdit)</div><div><br></div><div> viewMenu = self.menuBar().addMenu("&View")</div><div> viewSourceAction = QtGui.QAction("Page Source", self)</div>
<div> viewSourceAction.triggered.connect(self.fkViewSource)</div><div> viewMenu.addAction(viewSourceAction)</div><div><br></div><div> effectMenu = self.menuBar().addMenu("&Effect")</div>
<div> effectMenu.addAction("Highlight all links", self.highlightAllLinks)</div><div><br></div><div> self.rotateAction = QtGui.QAction(</div><div> self.style().standardIcon(</div><div>
QtGui.QStyle.SP_FileDialogDetailedView),</div><div> "Turn images upside down", self, checkable=True,</div><div> toggled=self.rotateImages)</div><div> effectMenu.addAction(self.rotateAction)</div>
<div><br></div><div> toolsMenu = self.menuBar().addMenu("&Tools")</div><div> toolsMenu.addAction("Remove GIF images", self.removeGifImages)</div><div> toolsMenu.addAction("Remove all inline frames",</div>
<div> self.removeInlineFrames)</div><div> toolsMenu.addAction("Remove all object elements",</div><div> self.removeObjectElements)</div><div> toolsMenu.addAction("Remove all embedded elements",</div>
<div> self.removeEmbeddedElements)</div><div> self.setCentralWidget(self.view)</div><div> self.setUnifiedTitleAndToolBarOnMac(True)</div><div><br></div><div> self.view.loadFinished.connect(self.fkSource)</div>
<div><br></div><div> def fkSource(self):</div><div> pass</div><div> </div><div> def fkViewSource(self):</div><div> self.textEdit = QtGui.QTextEdit(None)</div><div> self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)</div>
<div> self.textEdit.show()</div><div> self.textEdit.setPlainText(self.HTML)</div><div> self.textEdit.resize(600, 400)</div><div><br></div><div> def viewSource(self):</div><div> accessManager = self.view.page().networkAccessManager()</div>
<div> request = QtNetwork.QNetworkRequest(self.view.url())</div><div> reply = accessManager.get(request)</div><div> reply.finished.connect(self.slotSourceDownloaded)</div><div><br></div><div> def slotSourceDownloaded(self):</div>
<div> reply = self.sender()</div><div> self.textEdit = QtGui.QTextEdit(None)</div><div> self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)</div><div> self.textEdit.show()</div><div> self.textEdit.setPlainText(QtCore.QTextStream(reply).readAll())</div>
<div> self.textEdit.resize(600, 400)</div><div> reply.deleteLater()</div><div><br></div><div> def adjustLocation(self):</div><div> self.locationEdit.setText(self.view.url().toString())</div><div><br>
</div><div> def changeLocation(self):</div><div> url = QtCore.QUrl.fromUserInput(self.locationEdit.text())</div><div> self.view.load(url)</div><div> self.view.setFocus() </div><div><br></div><div> def adjustTitle(self):</div>
<div> if 0 < self.progress < 100:</div><div> self.setWindowTitle("%s (%s%%)" % (self.view.title(), self.progress))</div><div> else:</div><div> self.setWindowTitle(self.view.title())</div>
<div><br></div><div> def pokusnejs(self):</div><div> self.view.page().mainFrame().evaluateJavaScript(self.jQuery)</div><div> </div><div> def setProgress(self, p):</div><div> self.view.page().mainFrame().evaluateJavaScript(self.jQuery) </div>
<div><br></div><div> def finishLoading(self):</div><div> self.progress = 100</div><div> self.adjustTitle()</div><div> self.rotateImages(self.rotateAction.isChecked())</div><div><br></div><div> def highlightAllLinks(self):</div>
<div> code = """$('a').each(</div><div> function () {</div><div> $(this).css('background-color', 'yellow') </div><div> } </div>
<div> )"""</div><div> self.view.page().mainFrame().evaluateJavaScript(code)</div><div><br></div><div> def rotateImages(self, invert):</div><div> if invert:</div><div> code = """</div>
<div> $('img').each(</div><div> function () {</div><div> $(this).css('-webkit-transition', '-webkit-transform 2s'); </div><div> $(this).css('-webkit-transform', 'rotate(180deg)') </div>
<div> } </div><div> )"""</div><div> else:</div><div> code = """</div><div> $('img').each(</div><div> function () { </div>
<div> $(this).css('-webkit-transition', '-webkit-transform 2s'); </div><div> $(this).css('-webkit-transform', 'rotate(0deg)') </div><div> } </div>
<div> )"""</div><div><br></div><div> self.view.page().mainFrame().evaluateJavaScript(code)</div><div><br></div><div> def removeGifImages(self):</div><div> code = "$('[src*=gif]').remove()"</div>
<div> self.view.page().mainFrame().evaluateJavaScript(code)</div><div><br></div><div> def removeInlineFrames(self):</div><div> code = "$('iframe').remove()"</div><div> self.view.page().mainFrame().evaluateJavaScript(code)</div>
<div><br></div><div> def removeObjectElements(self):</div><div> code = "$('object').remove()"</div><div> self.view.page().mainFrame().evaluateJavaScript(code)</div><div><br></div><div> def removeEmbeddedElements(self):</div>
<div> code = "$('embed').remove()"</div><div> self.view.page().mainFrame().evaluateJavaScript(code)</div><div><br></div><div><br></div><div>if __name__ == '__main__':</div><div><br>
</div><div> import sys</div><div><br></div><div> app = QtGui.QApplication(sys.argv)</div><div><br></div><div> if len(sys.argv) > 1:</div><div> url = QtCore.QUrl(sys.argv[1])</div><div> else:</div><div>
url = QtCore.QUrl('<a href="http://domain.com">http://domain.com</a>')</div><div><br></div><div> browser = MainWindow(url)</div><div> browser.show()</div><div><br></div><div> sys.exit(app.exec_())</div>
</div><div><br></div></div>