如何在 QWebEngineView 中指定用户代理 [英] How to specify user agent in QWebEngineView

查看:63
本文介绍了如何在 QWebEngineView 中指定用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt5 在网页上实现自动化功能.PyQt5 中显示的页面与 Chrome 中显示的页面有很大不同.如果我要更改用户代理,我可以模仿 Chrome 的功能吗?如果是这样,我将如何更改以下示例中的用户代理:

I am working on automating functionality on a web page using PyQt5. The page that is displayed in PyQt5 is substantially different from what is displayed in Chrome. If I was to change the User Agent, could I mimic Chromes functionality? And if so, how would I go about changing the User Agent in the following example:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)
web = QWebEngineView()

profile = QWebEngineProfile()
profile.setHttpUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36")

# How do i set the profile in the web ???

web.load(QUrl("https://stackoverflow.com"))
web.show()
web.loadFinished.connect(on_load_finished)

sys.exit(app.exec_())

推荐答案

根据 文档:

User-Agent 请求标头包含一个特征字符串允许网络协议对等体识别应用程序类型,操作系统、软件供应商或软件版本请求软件用户代理.

The User-Agent request header contains a characteristic string that allows the network protocol peers to identify the application type, operating system, software vendor or software version of the requesting software user agent.

有些网页会使用用户代理为您的浏览器显示个性化内容,例如,您可以通过用户代理信息推断它是否支持 AJAX.

Some web pages will use the User Agent to display personalized content for your browser, for example, with the user-agent information you could deduce whether it supports AJAX or not.

如果我要更改用户代理,我可以模仿 Chrome 的功能吗?

可能是的,虽然Google Chrome和Qt Webengine都是基于chromium,但每个开发组都创建了一个新的层,可以有不同的功能,例如QtWebEngine抑制了新版本中添加的许多chromium功能.

Probably yes, although although Google Chrome and Qt Webengine are based on chromium, each development group has created a new layer that can have different functionalities, for example, QtWebEngine has suppressed many chromium functionalities that are added in the new versions.

我将如何更改用户代理?

不需要创建新的 QWebEngineProfile,因为您可以使用页面的配置文件:

It is not necessary to create a new QWebEngineProfile since you can use the profile of the page:

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication

if __name__ == "__main__":

    app = QApplication(sys.argv)
    web = QWebEngineView()

    print(web.page().profile().httpUserAgent())

    web.page().profile().setHttpUserAgent(
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
    )
    web.load(QUrl("https://stackoverflow.com"))
    web.show()
    web.resize(640, 480)
    sys.exit(app.exec_())

如果你想使用 QWebEngineProfile 然后创建一个新的 QWebEnginePage:

If you want to use the QWebEngineProfile then create a new QWebEnginePage:

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineView
from PyQt5.QtWidgets import QApplication

if __name__ == "__main__":

    app = QApplication(sys.argv)
    web = QWebEngineView()

    profile = QWebEngineProfile()
    profile.setHttpUserAgent(
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
    )

    page = QWebEnginePage(profile, web)
    web.setPage(page)
    web.load(QUrl("https://stackoverflow.com"))
    web.show()
    web.resize(640, 480)
    sys.exit(app.exec_())

这篇关于如何在 QWebEngineView 中指定用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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