1

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

1 に答える 1

2

PyQtでは、通常、複数のQtクラスからの継承は機能しません。したがって、仮想userAgentForUrl関数をオーバーライドするには、個別の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())
于 2012-11-24T19:37:12.313 に答える