def convert(args): profile = QWebEngineProfile() page = HeadlessPage(profile) settings = page.settings() settings.setAttribute(settings.JavascriptCanOpenWindows, False) settings.setAttribute(settings.WebGLEnabled, False) settings.setAttribute(settings.AutoLoadIconsForPage, False) settings.setAttribute(settings.ShowScrollBars, False) qurl = QUrl(args['url']) req = QWebEngineHttpRequest(qurl) for name, value in args.get('headers', ()): req.setHeader(name.encode('utf-8'), value.encode('utf-8')) store = page.profile().cookieStore() for name, value in args.get('cookies', ()): cookie = QNetworkCookie(name.encode('utf-8'), value.encode('utf-8')) cookie.setDomain(qurl.host()) store.setCookie(cookie) with prepare_loop(page.loadFinished): page.load(req) for js in args.get('run_script', ()): with prepare_loop() as loop: page.runJavaScript(js, lambda _: loop.quit()) if args['dest'] != '-': with prepare_loop(page.pdfPrintingFinished): page.printToPdf(os.path.abspath(args['dest'])) else: sys.stdout.buffer.write(page.printToPdfAndReturn())
def getReqSpecify(self): print("start") auth = get_auth_header() baseUrl = 'http://118.67.130.33:8000/credit/payment/' # dummy self.req = QWebEngineHttpRequest(url=QUrl(baseUrl)) self.req.setHeader(QByteArray().append('AUTH'), QByteArray().append(auth)) # dummy print(self.req) self.load(self.req)
def load(self, url): """Load the url and sets the request headers""" request = QWebEngineHttpRequest(QUrl(url)) headers = self._get_headers() for h, v in headers.items(): request.setHeader(bytearray(str(h), 'utf-8'), bytearray(str(v), 'utf-8')) self.view.load(request)
def webRequest(self): ''' @return: QWebEngineHttpRequest ''' if self._op == self.GetOperation: method = QWebEngineHttpRequest.Get else: method = QWebEngineHttpRequest.Post req = QWebEngineHttpRequest(self._url, method) req.setPostData(self._data) return req
def _request(self, url, method='get', **kwargs): req = QWebEngineHttpRequest(QUrl(url)) req.setMethod(QWebEngineHttpRequest.Post if method == 'post' else QWebEngineHttpRequest.Get) if 'headers' in kwargs: req.setHeader(self._set_headers(kwargs.get('headers'))) if 'body' in kwargs: req.setPostData(self._set_postdata(kwargs.get('body'))) return req
def loadDate(client, date): """Load the page for a date and blocks until the html is ready""" url = 'https://otv.karlsruhe.de/terminmodul/live/termin/index' dateStr = date.strftime('%Y-%m-%d') req = QWebEngineHttpRequest.postRequest(QUrl(url), { 'selectedDate': dateStr, 'baseUrl': '/terminmodul/live' }) client.myload(req)
class WebViewWidget(QWebEngineView): def __init__(self): super().__init__() self.getReqSpecify() def getReqSpecify(self): print("start") auth = get_auth_header() baseUrl = 'http://118.67.130.33:8000/credit/payment/' # dummy self.req = QWebEngineHttpRequest(url=QUrl(baseUrl)) self.req.setHeader(QByteArray().append('AUTH'), QByteArray().append(auth)) # dummy print(self.req) self.load(self.req) def printLoadStart(self): print("Start Loading") def printLoading(self): print("Loading") def printLoadFinished(self): print("Load Finished") def urlChangedFunction(self): self.setText(self.toString()) print("Url Changed") def btnBackFunc(self): self.back() def btnForwardFunc(self): self.forward() def btnRelaodFunc(self): self.reload() def btnStopFunc(self): self.stop()
def requests(self, payloads: str): for payload in payloads.splitlines(): url = self.request.url.replace("[X]", payload) url = QUrl.fromUserInput(url) req = QWebEngineHttpRequest(url) req.setMethod(QWebEngineHttpRequest.Get if self.request.method == "GET" else QWebEngineHttpRequest.Post) for header in self.request.headers: req.setHeader(header[0].encode(), header[1].encode()) if self.request.method == "POST" and self.request.post_data: req.setPostData(self.request.post_data.encode()) yield req
if __name__ == '__main__': client = Client() # load service selection page url = 'https://otv.karlsruhe.de/terminmodul/live/index/index/dienststelle/38' client.myload(QUrl(url)) # print(client.html) # choose application and load calendar page calendarUrl = 'https://otv.karlsruhe.de/terminmodul/live/dienstleistung/save' req = QWebEngineHttpRequest.postRequest( QUrl(calendarUrl), { 'dienstleistungsid[]': '460', 'personenzahl[460]': '1', 'personenzahl[458]': '1', 'bemerkung': '', 'anmeldung_action': '1', 'lang': 'de', 'weiter': 'Weiter: Terminauswahl' }) client.myload(req) # get all possible days (excluding weekends and holidays) within 90 days startDate = getSelectedDate(client.html) holidays = set( getHolidays(startDate) + getHolidays(startDate + datetime.timedelta(days=60))) possibleDates = [] for dayDelta in range(90): date = startDate + datetime.timedelta(days=dayDelta)
def ActMethon(self): if (self.plusInFunction != None): try: _url = '' # 主路径 mainUrl = '{0}://{1}'.format(self.plusIn.protocol, self.plusIn.url) _temp = re.search('(\{account\})+', mainUrl) if (_temp != None and len(_temp.groups()) > 0): mainUrl = re.sub('(\{account\})+', self.plusIn.account, mainUrl) _temp = re.search('(\{password\})+', mainUrl) if (_temp != None and len(_temp.groups()) > 0): mainUrl = re.sub('(\{password\})+', self.plusIn.password, mainUrl) # 子路径 if (self.plusInFunction.is_path == 1): # 局部子路径 subUrl = '{0}/{1}'.format(mainUrl, self.plusInFunction.url) else: # 完整子路径 subUrl = '{0}://{1}'.format( self.plusIn.protocol, self.plusInFunction.url) # 替换子路径中的账号及密码 _temp = re.search('(\{account\})+', subUrl) if (_temp != None and len(_temp.groups()) > 0): subUrl = re.sub('(\{account\})+', self.plusIn.account, subUrl) _temp = re.search('(\{password\})+', subUrl) if (_temp != None and len(_temp.groups()) > 0): subUrl = re.sub('(\{password\})+', self.plusIn.password, subUrl) # 如果是搜索类型路径 if (self.plusInFunction.field_type == 2): _temp = re.search('(\{.*?\})+', subUrl) if (_temp != None and len(_temp.groups()) > 0): # 需要正则匹配的路径 _url = QUrl( re.sub( '(\{' + self.plusInFunction.field + '\})+', self.objValue.text(), subUrl)) else: _url = QUrl('{0}?{1}={2}'.format( subUrl, self.plusInFunction.field, self.objValue.text())) if (_url == ''): _url = QUrl(subUrl) if (_url == ''): _url = QUrl(mainUrl) req = QWebEngineHttpRequest() req.setUrl(_url) if (self.plusInFunction.method == 'get'): req.setMethod(QWebEngineHttpRequest.Get) else: req.setMethod(QWebEngineHttpRequest.Post) self.webview.load(req) # self.app.log.info(_url) pass except Exception as e: self.app.log.info(e) QMessageBox.information(None, self._("sscode", "Run Error"), e, QMessageBox.Yes) else: QMessageBox.information(self, '操作提示', ' 动作配置有误', QMessageBox.Yes)
def initFields(self): # 动态动作类 BEGIN ======================== class ActBtDynamic: def __init__(self, mainWin, webview): self.plusIn = DbPlusIn() self.plusInFunction = DbPlusInFunction() self.objValue = object self.app = mainWin self.webview = webview pass def ActMethon(self): if (self.plusInFunction != None): try: _url = '' # 主路径 mainUrl = '{0}://{1}'.format(self.plusIn.protocol, self.plusIn.url) _temp = re.search('(\{account\})+', mainUrl) if (_temp != None and len(_temp.groups()) > 0): mainUrl = re.sub('(\{account\})+', self.plusIn.account, mainUrl) _temp = re.search('(\{password\})+', mainUrl) if (_temp != None and len(_temp.groups()) > 0): mainUrl = re.sub('(\{password\})+', self.plusIn.password, mainUrl) # 子路径 if (self.plusInFunction.is_path == 1): # 局部子路径 subUrl = '{0}/{1}'.format(mainUrl, self.plusInFunction.url) else: # 完整子路径 subUrl = '{0}://{1}'.format( self.plusIn.protocol, self.plusInFunction.url) # 替换子路径中的账号及密码 _temp = re.search('(\{account\})+', subUrl) if (_temp != None and len(_temp.groups()) > 0): subUrl = re.sub('(\{account\})+', self.plusIn.account, subUrl) _temp = re.search('(\{password\})+', subUrl) if (_temp != None and len(_temp.groups()) > 0): subUrl = re.sub('(\{password\})+', self.plusIn.password, subUrl) # 如果是搜索类型路径 if (self.plusInFunction.field_type == 2): _temp = re.search('(\{.*?\})+', subUrl) if (_temp != None and len(_temp.groups()) > 0): # 需要正则匹配的路径 _url = QUrl( re.sub( '(\{' + self.plusInFunction.field + '\})+', self.objValue.text(), subUrl)) else: _url = QUrl('{0}?{1}={2}'.format( subUrl, self.plusInFunction.field, self.objValue.text())) if (_url == ''): _url = QUrl(subUrl) if (_url == ''): _url = QUrl(mainUrl) req = QWebEngineHttpRequest() req.setUrl(_url) if (self.plusInFunction.method == 'get'): req.setMethod(QWebEngineHttpRequest.Get) else: req.setMethod(QWebEngineHttpRequest.Post) self.webview.load(req) # self.app.log.info(_url) pass except Exception as e: self.app.log.info(e) QMessageBox.information(None, self._("sscode", "Run Error"), e, QMessageBox.Yes) else: QMessageBox.information(self, '操作提示', ' 动作配置有误', QMessageBox.Yes) # 动态动作类 END ======================== try: # 首页 mainUrl = '{0}://{1}'.format(self.plusin.protocol, self.plusin.url) _temp = re.search('(\{account\})+', mainUrl) if (_temp != None and len(_temp.groups()) > 0): mainUrl = re.sub('(\{account\})+', self.plusin.account, mainUrl) _temp = re.search('(\{password\})+', mainUrl) if (_temp != None and len(_temp.groups()) > 0): mainUrl = re.sub('(\{password\})+', self.plusin.password, mainUrl) req = QWebEngineHttpRequest() req.setUrl(QUrl(mainUrl)) if (self.plusin.method == 'get'): req.setMethod(QWebEngineHttpRequest.Get) else: req.setMethod(QWebEngineHttpRequest.Post) self.webview.load(req) # cookie token self.webview.page().profile().defaultProfile().cookieStore( ).setCookie( QNetworkCookie(QByteArray("token".encode()), QByteArray(self.mainWin.app.token.encode())), QUrl(mainUrl)) # auto login self.webview.page().loadFinished.connect(self.sendLoginInfo) icon = QtGui.QIcon() icon.addPixmap( QtGui.QPixmap(":/button/images/svg/angle-right-solid.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off) if (self.plusinFunction and len(self.plusinFunction) > 0): lineRows = 5 for index in range(len(self.plusinFunction)): # 动态方法 Begin =============== def setActMethon(self, plusin, plusinFunction, objValue): self.plusIn = plusin self.plusInFunction = plusinFunction self.objValue = objValue _act = ActBtDynamic(self, self.webview) _act.setActMethon = types.MethodType(setActMethon, _act) # 动态方法 End =============== if (self.plusinFunction[index].field_type == 2): self.labelFields.append( QtWidgets.QLabel(self.widgetNav)) self.labelFields[index].setFrameShape( QtWidgets.QFrame.NoFrame) self.labelFields[index].setFrameShadow( QtWidgets.QFrame.Sunken) self.labelFields[index].setObjectName( "labelField_{0}".format(index)) if (self.mainWin.app.configs.language.value == '简体中文'): self.labelFields[index].setText("{0}".format( self.plusinFunction[index].title_cn)) else: self.labelFields[index].setText("{0}".format( self.plusinFunction[index].title_en)) self.formLayout.setWidget( lineRows, QtWidgets.QFormLayout.SpanningRole, self.labelFields[index]) lineRows += 1 self.leFields.append( QtWidgets.QLineEdit(self.widgetNav)) self.leFields[index].setObjectName( "leField_{0}".format(index)) self.leFields[index].setFrame(True) self.leFields[index].setClearButtonEnabled(True) # 动作信号绑定 _act.setActMethon(self.plusin, self.plusinFunction[index], self.leFields[index]) self.actBtDynamicClicked.append(_act) self.leFields[index].returnPressed.connect( self.actBtDynamicClicked[index].ActMethon) self.formLayout.setWidget( lineRows, QtWidgets.QFormLayout.LabelRole, self.leFields[index]) self.tbGos.append(QtWidgets.QToolButton( self.widgetNav)) self.tbGos[index].setCursor( QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.tbGos[index].setText("") self.tbGos[index].setIcon(icon) self.tbGos[index].setPopupMode( QtWidgets.QToolButton.InstantPopup) self.tbGos[index].setAutoRaise(True) self.tbGos[index].setObjectName( "tbGo_{0}".format(index)) self.tbGos[index].setToolTip(self._("WebForm", "Go")) self.formLayout.setWidget( lineRows, QtWidgets.QFormLayout.FieldRole, self.tbGos[index]) # 动作信号绑定 self.tbGos[index].clicked.connect( self.actBtDynamicClicked[index].ActMethon) lineRows += 1 self.pbGos.append('') else: self.pbGos.append(QtWidgets.QPushButton( self.widgetNav)) sizePolicy = QtWidgets.QSizePolicy( QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.pbGos[index].sizePolicy().hasHeightForWidth()) self.pbGos[index].setSizePolicy(sizePolicy) self.pbGos[index].setCursor( QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pbGos[index].setIcon(icon) self.pbGos[index].setAutoDefault(False) self.pbGos[index].setDefault(True) self.pbGos[index].setFlat(True) self.pbGos[index].setObjectName( "pbGo_{0}".format(index)) if (self.mainWin.app.configs.language.value == '简体中文'): self.pbGos[index].setText("{0}".format( self.plusinFunction[index].title_cn)) else: self.pbGos[index].setText("{0}".format( self.plusinFunction[index].title_en)) self.formLayout.setWidget( lineRows, QtWidgets.QFormLayout.SpanningRole, self.pbGos[index]) # 菜单动作信号绑定 _act.setActMethon(self.plusin, self.plusinFunction[index], None) self.actBtDynamicClicked.append(_act) self.pbGos[index].clicked.connect( self.actBtDynamicClicked[index].ActMethon) lineRows += 1 self.labelFields.append('') self.leFields.append('') self.tbGos.append('') else: self.widgetNav.close() except Exception as e: print(e) self.widgetNav.update() pass