1
class MyWebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, parent=None):
        super().__init__(parent)

    def interceptRequest(self, info):
        print("interceptRequest")
        print(info.requestUrl())

class MyWebEnginePage(QWebEnginePage):

    def acceptNavigationRequest(self, url, _type, isMainFrame):
        print("acceptNavigationRequest")
        print(url)
        return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)

     def certificateError(self, certificateError):
       print(certificateError.errorDescription(), certificateError.url(), certificateError.isOverridable())
       error = certificateError.error()

       return super(MyWebEnginePage, self).certificateError(certificateError)    

class Browser(QMainWindow):
    proxy_type = QNetworkProxy.HttpProxy
    proxy_hostname = 'proxy_hostname'
    proxy_port = 64777
    proxy_username = 'proxy_username'
    proxy_password = 'proxy_username'

    def __init__(self, url):
        super().__init__()
        self.searching_url = url
        self.set_auth_proxy()
        self.webEngineView = QWebEngineView()

        self.setMinimumSize(1200, 600)
        self.setCentralWidget(self.webEngineView)

        interceptor = MyWebEngineUrlRequestInterceptor()

        profile = QWebEngineProfile('storage', self.webEngineView)

        profile.setRequestInterceptor(interceptor)

        url = QUrl(self.searching_url)
        url.setScheme("http")
        url.setHost("www.bet365.com")
        httpReq = QWebEngineHttpRequest()
        httpReq.setUrl(url)
        httpReq.setMethod(QWebEngineHttpRequest.Get)
        httpReq.setHeader(QByteArray(b'Accept'), QByteArray(b'*/*'))
        
        httpReq.setHeader(QByteArray(b'Content-Type'), QByteArray(b'application/json'))
        httpReq.setHeader(QByteArray(b'User-Agent'), QByteArray(b'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'))

        print(httpReq.header(QByteArray(b'Host')))

        page = MyWebEnginePage(profile, self.webEngineView)
        self.webEngineView.setPage(page)

        self.show()
        self.webEngineView.load(httpReq)

    def set_auth_proxy(self):
        self.auth_proxy = QNetworkProxy()
        self.auth_proxy.setType(self.proxy_type)
        self.auth_proxy.setHostName(self.proxy_hostname)
        self.auth_proxy.setPort(self.proxy_port)
        self.auth_proxy.setUser(self.proxy_username)
        self.auth_proxy.setPassword(self.proxy_password)
        self.auth_proxy.setApplicationProxy(self.auth_proxy)



if __name__ == '__main__':
    bet365 = 'https://www.bet365.com'    
    postman = 'http://postman-echo.com/get'

    app = QApplication(sys.argv)
    browser = Browser(bet365)
    sys.exit(app.exec_())

这是浏览器代码,它应该只是打开页面 bet365.com 但问题是页面没有加载,程序窗口变成白色,而其他站点,如 2ip.ru 已加载并显示我的 IP。我该怎么做才能让 bet365 正确回答我?

白窗截图:
白窗截图

另外,当我像这样设置 Host 标头时 httpReq.setHeader(QByteArray(b'Host'), QByteArray(b'www.bet365.com')),代码 ERR_INVALID_ARGUMENT 出现错误,我认为在请求时它仍然与标头有关,但不,我设置了不同的标头并且没有运气。

4

0 回答 0