使用 QWebEnginePage 时出现奇怪的 JS 错误
Odd JS Errors when using QWebEnginePage
所以我一直在使用 PyQt6-WebEngine 开发一个身份验证系统来发出将遵循 javascript 重定向的 Web 请求,因为 Python 中的请求不支持它。我做了一个简单的请求脚本:
import sys
import json
# import PyQt6
import PyQt6
from PyQt6.QtCore import QUrl, QByteArray
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineCore import QWebEnginePage, QWebEngineHttpRequest
class __Page__(QWebEnginePage):
def __init__(self, app, url=None, method=None, headers=None, data=None) -> None:
# Initiate the application
self.app = app
super().__init__()
# Make sure the necessary variables aren't empty
if url == None:
raise ValueError("Passed url variable cannot be empty")
if method == None:
raise ValueError("Passed method variable cannot be empty")
# Creates the main request variable
self.req = QWebEngineHttpRequest()
# Assign the variables used to setup the request later in self.setupRequest()
self.method = method
self.qurl = url
self.headers = headers
# Only assign this if the method is post
if self.method == "post":
self.postData = data
else:
self.postData = None
# Setup the request
self.setupRequest()
# Create a new html variable and stuff, then load the request and launch the app
self.html = ""
self.loadFinished.connect(self.finish)
self.load(self.req)
self.app.exec()
def setupRequest(self):
# Assign the method variable
if self.method == "get":
method = QWebEngineHttpRequest.Method.Get
elif self.method == "post":
method = QWebEngineHttpRequest.Method.Post
else:
raise ValueError(
"Expected 'get' or 'post' methods, but received '{}'".format(
self.method
)
)
# Assign the method variable to the request method
self.req.setMethod(method)
self.req.setUrl(QUrl(self.qurl))
# Assign the headers
if self.headers != None:
for i in self.headers:
self.req.setHeader(
QByteArray(list(i.keys())[0].encode("ascii")),
QByteArray(list(i.values())[0].encode("ascii")),
)
# Assign the postData if the method is post
if self.method == "post":
self.req.setPostData(bytes(self.postData, "utf-8"))
# This part is a bit over my head
def finish(self):
self.html = self.toHtml(self.callHtml)
self.url = self.url()
self.deleteLater()
self.app.quit()
def callHtml(self, html_str):
self.html = html_str
class app:
def __init__(self) -> None:
self.app = QApplication(sys.argv)
def requestPage(self, url=None, method=None, headers=None, data=None):
page = __Page__(
app=self.app, url=url, method=method, headers=headers, data=data
)
return page
def main():
webapp = app()
request = webapp.requestPage(url="https://google.com", method="get")
if __name__ == "__main__":
main()
这个问题是它一直在输出中给我 JS 错误,它突然开始出现并且没有明显的原因。使用 Google.com 它给出 1-2 个错误,例如: js: Error with Permissions-Policy header: Unrecognized feature: 'ch-ua-full-version-list'.
对于其他网站,它给了我 30+。
除了混乱之外,这本身并没有那么烦人,但我遇到了另一个问题(我不太确定是这个脚本的错,但它与JS 错误问题)请求的网站根本不会 return 任何 html。
有什么想法吗?
我终于解决了这个问题:JS错误与请求失败无关。我刚刚提出了无效请求(header 问题和问题)。我仍然不确定为什么错误似乎随机弹出,也许我只是错误地得出了这个结论。
所以我一直在使用 PyQt6-WebEngine 开发一个身份验证系统来发出将遵循 javascript 重定向的 Web 请求,因为 Python 中的请求不支持它。我做了一个简单的请求脚本:
import sys
import json
# import PyQt6
import PyQt6
from PyQt6.QtCore import QUrl, QByteArray
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineCore import QWebEnginePage, QWebEngineHttpRequest
class __Page__(QWebEnginePage):
def __init__(self, app, url=None, method=None, headers=None, data=None) -> None:
# Initiate the application
self.app = app
super().__init__()
# Make sure the necessary variables aren't empty
if url == None:
raise ValueError("Passed url variable cannot be empty")
if method == None:
raise ValueError("Passed method variable cannot be empty")
# Creates the main request variable
self.req = QWebEngineHttpRequest()
# Assign the variables used to setup the request later in self.setupRequest()
self.method = method
self.qurl = url
self.headers = headers
# Only assign this if the method is post
if self.method == "post":
self.postData = data
else:
self.postData = None
# Setup the request
self.setupRequest()
# Create a new html variable and stuff, then load the request and launch the app
self.html = ""
self.loadFinished.connect(self.finish)
self.load(self.req)
self.app.exec()
def setupRequest(self):
# Assign the method variable
if self.method == "get":
method = QWebEngineHttpRequest.Method.Get
elif self.method == "post":
method = QWebEngineHttpRequest.Method.Post
else:
raise ValueError(
"Expected 'get' or 'post' methods, but received '{}'".format(
self.method
)
)
# Assign the method variable to the request method
self.req.setMethod(method)
self.req.setUrl(QUrl(self.qurl))
# Assign the headers
if self.headers != None:
for i in self.headers:
self.req.setHeader(
QByteArray(list(i.keys())[0].encode("ascii")),
QByteArray(list(i.values())[0].encode("ascii")),
)
# Assign the postData if the method is post
if self.method == "post":
self.req.setPostData(bytes(self.postData, "utf-8"))
# This part is a bit over my head
def finish(self):
self.html = self.toHtml(self.callHtml)
self.url = self.url()
self.deleteLater()
self.app.quit()
def callHtml(self, html_str):
self.html = html_str
class app:
def __init__(self) -> None:
self.app = QApplication(sys.argv)
def requestPage(self, url=None, method=None, headers=None, data=None):
page = __Page__(
app=self.app, url=url, method=method, headers=headers, data=data
)
return page
def main():
webapp = app()
request = webapp.requestPage(url="https://google.com", method="get")
if __name__ == "__main__":
main()
这个问题是它一直在输出中给我 JS 错误,它突然开始出现并且没有明显的原因。使用 Google.com 它给出 1-2 个错误,例如: js: Error with Permissions-Policy header: Unrecognized feature: 'ch-ua-full-version-list'.
对于其他网站,它给了我 30+。
除了混乱之外,这本身并没有那么烦人,但我遇到了另一个问题(我不太确定是这个脚本的错,但它与JS 错误问题)请求的网站根本不会 return 任何 html。
有什么想法吗?
我终于解决了这个问题:JS错误与请求失败无关。我刚刚提出了无效请求(header 问题和问题)。我仍然不确定为什么错误似乎随机弹出,也许我只是错误地得出了这个结论。