Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting useragent in QWebView

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/'))
like image 994
Matt Avatar asked Jul 17 '10 19:07

Matt


2 Answers

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.

like image 154
mechimdi Avatar answered Oct 03 '22 02:10

mechimdi


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())
like image 23
Vor Avatar answered Oct 03 '22 02:10

Vor