Need help override a function QWebPage.userAgentFo

2019-08-15 08:25发布

I want to override the userAgentForUrl function of QWebPage class, but I'm doing something wrong, and the user agent is still the default one.

#! /usr/bin/env python2.7

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys
from bs4 import BeautifulSoup

class Browser(QWebView, QWebPage):

    def __init__(self):
        QWebView.__init__(self)
        QWebPage.__init__(self)
        self.frame = self.page().mainFrame()
        self.loadFinished.connect(self.print_html)
        self.loadProgress.connect(self.print_progress)

    def userAgentForUrl(self, url):
        return "Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"

    def print_progress(self, percent):
        print percent

    def print_html(self):
        print "Done"
        self.fill_form()
        html = unicode(self.frame.toHtml()).encode('utf-8')
        soup = BeautifulSoup(html)
        print soup.prettify()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    br = Browser()
    br.load(QUrl('http://www.useragentstring.com/'))
    br.show()
    app.exec_()

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-15 08:57
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PySide import QtGui
from PySide import QtWebKit

app = QtGui.QApplication(sys.argv)

user_agent = QtWebKit.QWebPage()
user_agent.userAgentForUrl = lambda x: 'Firefox/17.0'

webkit = QtWebKit.QWebView()
webkit.setPage(user_agent)
webkit.load('http://www.whatsmyuseragent.com')
webkit.show()

app.exec_()
查看更多
乱世女痞
3楼-- · 2019-08-15 09:01

In PyQt, inheriting from multiple Qt classes usually won't work. So you will need a separate QWebPage subclass in order to override the virtual userAgentForUrl function.

Try something like this:

class WebPage(QWebPage):
    def userAgentForUrl(self, url):
        return "Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"

class Browser(QWebView):
    def __init__(self):
        QWebView.__init__(self)
        self.setPage(WebPage())
查看更多
登录 后发表回答