在QWebView中设置useragent [英] Setting useragent in QWebView

查看:265
本文介绍了在QWebView中设置useragent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QWebView,它工作正常.然后,使用来自spynner的代码,我尝试将useragent方法绑定到自定义方法.这似乎可以在spynner(与QWebPage一起)中工作,但不适用于此.任何帮助,不胜感激.代码:

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/'))

推荐答案

我希望这会有所帮助...

I hope this helps...

您的代码

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/'))

先决条件依赖性

from PyQt4.QtWebKit import * # Import all from QtWebKit

上一个指令允许一个人继承使用QtWebKit.QWebKit()对象及其方法.但是您缺少另一个组件,可以用来指定用户代理("Web浏览器").注意,上面我写了QWebView.load方法的签名

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

碰巧的是,您在致电时正在使用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

连接点

好吧,现在让我们把它们放在一起.我们知道QUrl是QNetworkRequest()对象,我们可以使用QNetworkRequest指定URL.我们需要知道的最后一件事是如何设置用户代理.

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.

使用QNetworkRequest的方法setRawHeader(string,string)设置用户代理

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

完成!

最终草案

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.

这篇关于在QWebView中设置useragent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆