Setting useragent in QWebView

给你一囗甜甜゛ 提交于 2019-12-04 14:31:41

问题


I have a QWebView, which works fine. Then, using code from spynner, I attempt to bind the useragent method to a custom method. This appears to work in spynner (with a QWebPage), but not here. Any help much appreciated. Code:

def customuseragent(url):
 print 'called for %s' % url
 return 'custom ua'


#inside a class
self.webkit = QtWebKit.QWebView()
self.webkit.page().userAgentForUrl = customuseragent
self.webkit.load(QtCore.QUrl('http://www.whatsmyuseragent.com/'))

回答1:


I hope this helps...

Your Code

def customuseragent(url):
    print 'called for %s' % url
    return 'custom ua'


#inside a class
self.webkit = QtWebKit.QWebView()
self.webkit.page().userAgentForUrl = customuseragent
self.webkit.load(QtCore.QUrl('http://www.whatsmyuseragent.com/'))

Prerequisite Dependencies

from PyQt4.QtWebKit import * # Import all from QtWebKit

The previous directive allows one to inherit use of the QtWebKit.QWebKit() object and it's methods. But you're missing one more component that allows you to specify the User Agent ("Web browser"). Notice that above I wrote out the signature for the QWebView.load method

QWebView.load(QNetworkRequest var) # Where var is a variable object of QNetworkRequest

It just so happens that you're using QNetworkRequest when you call

QtCore.QUrl('http://www.whatsmyuseragent.com/')

So technically the above line is the same as the following:

self.request = QNetworkRequest()
self.request.setUrl(QUrl(url))

In order to include the above lines, you need to import:

from PyQt4.QtNetwork import * # Just import all to be lazy

OR

from PyQt4.QtNetwork import QNetworkRequest # This is actually the origin of QNetworkRequest

Connect the Dots

Ok, let's pull it all together now. We understand that QUrl is a QNetworkRequest() object and we can specify the url using QNetworkRequest. The last thing we need to know is how to set the User Agent.

User Agent is set using the setRawHeader(string, string) a method of QNetworkRequest

self.request.setRawHeader("User-Agent","You/desired/user/agent")
self.request.load(self.request) #load the QNetworkRequest object variable to .load()

DONE!

Final Draft

from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *

USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1"


def customuseragent(url):
    print 'called for %s' % url
    return 'custom ua'


#inside a class
# class WebRequest(QWebView) ## the definition of the class uncomment to make use of the inheritance.

## from this tutorial
self.request = QNetworkRequest()
self.request.setUrl(QUrl(url))
self.request.setRawHeader("User-Agent",USER_AGENT)

## modified version of the original design
self.webkit = QtWebKit.QWebView()
self.webkit.page().userAgentForUrl = customuseragent
self.webkit.load(self.request)

I hope this helped you. I left out a few things because I think you get the fundamentals.




回答2:


class MyBrowser(QWebPage):
    ''' Settings for the browser.'''

    def userAgentForUrl(self, url):
        ''' Returns a User Agent that will be seen by the website. '''
        return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15"

class Browser(QWebView):
    def __init__(self):
        QWebView.__init__(self)
        self.setPage(MyBrowser())


来源:https://stackoverflow.com/questions/3273011/setting-useragent-in-qwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!