在PyQtWebkit中为QNetworkRequest设置User-Agent,可以通过以下步骤实现:
pip install PyQt5
pip install PyQtWebEngine
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage
class CustomWebPage(QWebEnginePage):
def __init__(self, parent=None):
super(CustomWebPage, self).__init__(parent)
def createRequest(self, operation, request, outgoingData):
request.setHeader(QNetworkRequest.UserAgentHeader, "MyCustomUserAgent")
return super(CustomWebPage, self).createRequest(operation, request, outgoingData)
app = QApplication(sys.argv)
web_page = CustomWebPage()
web_page.load(QUrl("https://www.example.com"))
web_page.show()
sys.exit(app.exec_())
这样,在PyQtWebkit中为QNetworkRequest设置User-Agent的问题就得到了解决。
领取专属 10元无门槛券
手把手带您无忧上云