什么是 PyQt Web kit Qwebview 的默认用户代理以及如何获取它 [英] what is the Default user-Agent of PyQt Web kit Qwebview and how to get it

查看:54
本文介绍了什么是 PyQt Web kit Qwebview 的默认用户代理以及如何获取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 python 的新手,并在 PyQt 中开发了一个 GUI,它有一个 Web 浏览器.我想显示用户代理与 Url 一起使用但不创建方式.我的代码是 -

i am new to python and developing a GUI in PyQt which has a Web Browser. I want to show the User-Agent going with the Url but not founding a way.my code is -

class Manager(QNetworkAccessManager):
def __init__(self, table):
    QNetworkAccessManager.__init__(self)
    self.finished.connect(self._finished)
    self.table = table

def _finished(self, reply):
    headers = reply.rawHeaderPairs()
    headers = {str(k):str(v) for k,v in headers}
    content_type = headers.get("Content-Type")

    # some code like "print headers.get("User-Agent")"

    url = reply.url().toString()
    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    status, ok = status.toInt()
    self.table.update([url, str(status), content_type])

目前,上面的代码只显示了 URL、状态和内容类型,但我也想显示用户代理.有人知道吗?

Presently, the above code is showing only the URL,status and content type , but with this i also wants to display user agent.do someone has any idea?

推荐答案

User-Agent 是发送到服务器的东西.此信息不是从服务器发送的.

A User-Agent is something which gets send to the server. This information is not sent from the server.

要设置用户代理,您可以使用 Manager 类执行以下操作,例如:

To set a user agent you can do the following with your Manager class for example:

from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

manager = Manager()
request = QNetworkRequest(QUrl("http://www.google.com/"))
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")
manager.get(request)

并修改您的 def _finished(self, reply): 方法以获取带有 User-Agent 的请求:

And modify your def _finished(self, reply): method to get the request with the User-Agent:

def _finished(self, reply):
    print reply.request().rawHeader("User-Agent")

这篇关于什么是 PyQt Web kit Qwebview 的默认用户代理以及如何获取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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