class Page(QWebEngineView): """ class which define pages of navigator """ def __init__(self, parent, url=""): QWebEngineView.__init__(self, parent) self.navigator = parent self.webProfile = QWebEngineProfile(self) self.origineUrl = url self.currentUrl = url self.load(url) self.urlChanged.connect(self.on_urlChanged) def load(self, url: str): """Load an url in this page""" url = self.reformUrl(url) self.webProfile.clearHttpCache() self.setUrl(QUrl(url)) self.currentUrl = url QWebEngineView.load(self, QUrl(url)) def on_urlChanged(self, url): """When the page is changed by a link or other action""" self.currentUrl = url.toString() self.navigator.window.setWindowTitle("VPBrowser - " + self.currentUrl) @staticmethod def reformUrl(url: str): if url.lower().find("localhost") == 0: #If the project is local url = "http://" + url return url
def getProfile(self): path = constants.QWEBENGINECACHE_PATH tryMkdir(path) profile = QWebEngineProfile() profile.setCachePath(path) profile.clearHttpCache() jsFile = constants.QTWEBCHANNELJS_FILE with open(jsFile, encoding="UTF-8") as file: js = file.read() script = QWebEngineScript() script.setSourceCode(js) script.setName('qwebchannel.js') script.setInjectionPoint(QWebEngineScript.DocumentCreation) script.setWorldId(QWebEngineScript.MainWorld) script.setRunsOnSubFrames(False) profile.scripts().insert(script) return profile
ip = res.text.split(":") port = ip[1] ip = ip[0] proxy = QtNetwork.QNetworkProxy() # Http访问代理 proxy.setType(QtNetwork.QNetworkProxy.HttpProxy) # 代理ip地址HttpProxy proxy.setHostName(ip) # 端口号 proxy.setPort(int(port)) QtNetwork.QNetworkProxy.setApplicationProxy(proxy) interceptor = TwitchInterceptor() profile = QWebEngineProfile() profile.clearHttpCache() profile.clearAllVisitedLinks() pCookie = profile.cookieStore() pCookie.deleteAllCookies() pCookie.deleteSessionCookies() profile.setRequestInterceptor(interceptor) profile.setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies) page = QWebEnginePage(profile, view) page.loadFinished.connect(on_done) view.setPage(page) page.setUrl(QUrl(twitch_url)) view.show() sys.exit(app.exec_())