Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要帮助重写QWebPage.userAgentForUrl()函数_Python_Qt_Inheritance_Pyqt4 - Fatal编程技术网

Python 需要帮助重写QWebPage.userAgentForUrl()函数

Python 需要帮助重写QWebPage.userAgentForUrl()函数,python,qt,inheritance,pyqt4,Python,Qt,Inheritance,Pyqt4,我想重写QWebPage类的userAgentForUrl函数,但我做错了,用户代理仍然是默认的 #! /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

我想重写QWebPage类的userAgentForUrl函数,但我做错了,用户代理仍然是默认的

#! /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_()

在PyQt中,从多个Qt类继承通常不起作用。因此,您需要一个单独的QWebPage子类来覆盖虚拟函数

试着这样做:

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())