欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例

 更新時(shí)間:2019年02月13日 10:58:27   作者:李毅  
今天小編就為大家分享一篇關(guān)于PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

概要

應(yīng)同學(xué)邀請(qǐng),演示如何使用 PyQt5 內(nèi)嵌瀏覽器瀏覽網(wǎng)頁(yè),并注入 Javascript 腳本實(shí)現(xiàn)自動(dòng)化操作。

下面測(cè)試的是一個(gè)廉價(jià)機(jī)票預(yù)訂網(wǎng)站(http://www.flyscoot.com/),關(guān)鍵點(diǎn)如下

  1. 使用 QWebEngineView 加載網(wǎng)頁(yè),并顯示進(jìn)度。
  2. 在默認(rèn)配置(QWebEngineProfile)中植入 Javascript 內(nèi)容,這樣腳本會(huì)在所有打開(kāi)的網(wǎng)頁(yè)中執(zhí)行,不論跳轉(zhuǎn)到哪個(gè)網(wǎng)址。
  3. Javascript 腳本使用網(wǎng)址中的路徑名,判斷當(dāng)前網(wǎng)頁(yè)位置,從而決定執(zhí)行哪種操作。

python 代碼示例

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''使用 PyQt5 內(nèi)嵌瀏覽器瀏覽網(wǎng)頁(yè),并注入 Javascript 腳本實(shí)現(xiàn)自動(dòng)化操作。'''
import os
import sys
from datetime import datetime
from PyQt5.QtWidgets import (
  QWidget, QApplication, QVBoxLayout, QHBoxLayout,
  QDesktopWidget, QTextEdit, QLabel, QLineEdit, QPushButton,
  QFileDialog, QProgressBar,
)
from PyQt5.QtCore import QUrl, pyqtSlot
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEngineScript, QWebEnginePage
class Browser(QWidget):
  def __init__(self):
    super().__init__()
    self.init_ui()
    # 腳本
    self.profile = QWebEngineProfile.defaultProfile()
    self.script = QWebEngineScript()
    self.prepare_script()
  def init_ui(self):
    self.webView = QWebEngineView()
    self.logEdit = QTextEdit()
    self.logEdit.setFixedHeight(100)
    self.addrEdit = QLineEdit()
    self.addrEdit.returnPressed.connect(self.load_url)
    self.webView.urlChanged.connect(
      lambda i: self.addrEdit.setText(i.toDisplayString()))
    self.jsEdit = QLineEdit()
    self.jsEdit.setText('inject.js')
    loadUrlBtn = QPushButton('加載')
    loadUrlBtn.clicked.connect(self.load_url)
    chooseJsBtn = QPushButton('選擇腳本文件')
    chooseJsBtn.clicked.connect(self.choose_js_file)
    # 導(dǎo)航/工具
    top = QWidget()
    top.setFixedHeight(80)
    topBox = QVBoxLayout(top)
    topBox.setSpacing(0)
    topBox.setContentsMargins(5, 0, 0, 5)
    progBar = QProgressBar()
    progBox = QHBoxLayout()
    progBox.addWidget(progBar)
    topBox.addLayout(progBox)
    naviBox = QHBoxLayout()
    naviBox.addWidget(QLabel('網(wǎng)址'))
    naviBox.addWidget(self.addrEdit)
    naviBox.addWidget(loadUrlBtn)
    topBox.addLayout(naviBox)
    naviBox = QHBoxLayout()
    naviBox.addWidget(QLabel('注入腳本文件'))
    naviBox.addWidget(self.jsEdit)
    naviBox.addWidget(chooseJsBtn)
    topBox.addLayout(naviBox)
    self.webView.loadProgress.connect(progBar.setValue)
    # 主界面
    layout = QVBoxLayout(self)
    layout.addWidget(self.webView)
    layout.addWidget(top)
    layout.addWidget(self.logEdit)
    self.show()
    self.resize(1024, 900)
    self.center()
  def center(self):
    qr = self.frameGeometry()
    cp = QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())
  @pyqtSlot()
  def load_url(self):
    url = self.addrEdit.text().strip()
    if not url.lower().startswith('http://') \
        and not url.lower().startswith('https://'):
      url = 'http://{}'.format(url)
    self.load(url)
  @pyqtSlot()
  def choose_js_file(self):
    f, _ = QFileDialog.getOpenFileName(filter="Javascript files(*.js)")
    if os.path.isfile(f):
      self.jsEdit.setText(f)
      self.prepare_script()
  def prepare_script(self):
    path = self.jsEdit.text().strip()
    if not os.path.isfile(path):
      self.log('invalid js path')
      return
    self.profile.scripts().remove(self.script)
    with open(path, 'r') as f:
      self.script.setSourceCode(f.read())
    self.profile.scripts().insert(self.script)
    self.log('injected js ready')
  def log(self, msg, *args, **kwargs):
    m = msg.format(*args, **kwargs)
    self.logEdit.append('{} {}'.format(
      datetime.now().strftime('%H:%M:%S'), m))
  def load(self, url):
    self.log(f'loading {url}')
    self.addrEdit.setText(url)
    self.webView.load(QUrl(url))
if __name__ == '__main__':
  app = QApplication(sys.argv)
  b = Browser()
  b.load('http://www.flyscoot.com/')
  sys.exit(app.exec_())

Javascript 腳本示例

// 簡(jiǎn)單起見(jiàn),這里只演示部分頁(yè)面,腳本內(nèi)容摘自 Heng丶原貼文。
function handle(path) {
  // 首頁(yè)
  if (path == '/zh') {
    document.getElementsByClassName('radio-inline')[1].click();
    document.getElementById('oneway_from').value='廣州 (CAN)';
    document.getElementById('oneway_to').value='新加坡 (SIN)';
    document.getElementById('oneway_departuredate').value='2018年9月10日';
    document.getElementsByClassName('btn--booking')[1].click();
    return;
  }
  // 選擇航班
  if (path == '/Book/Flight') {
    document.getElementsByClassName('price--sale')[0].click();
    document.getElementsByClassName('heading-4')[0].click();
    document.getElementsByClassName('btn-submit')[0].click();
    return;
  }
  // 乘客信息
  if (path == '/BookFlight/Passengers') {
    document.getElementsByClassName('fname1')[0].value = "匿名";
  }
}
let host = document.location.hostname;
if (host.endsWith('.flyscoot.com')) {
  handle(document.location.pathname);
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • python3 wechatpy微信支付的項(xiàng)目實(shí)踐

    python3 wechatpy微信支付的項(xiàng)目實(shí)踐

    本文主要介紹了python3 wechatpy微信支付的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn)

    關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn)

    今天小編就為大家分享一篇關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • Python的自動(dòng)化部署模塊Fabric的安裝及使用指南

    Python的自動(dòng)化部署模塊Fabric的安裝及使用指南

    這篇文章主要介紹了Python的自動(dòng)化部署模塊Fabric的安裝及使用指南,文中以Debian系統(tǒng)為環(huán)境進(jìn)行了實(shí)例演示,需要的朋友可以參考下
    2016-01-01
  • python操作sqlite的CRUD實(shí)例分析

    python操作sqlite的CRUD實(shí)例分析

    這篇文章主要介紹了python操作sqlite的CRUD實(shí)現(xiàn)方法,涉及Python操作SQLite數(shù)據(jù)庫(kù)CURD相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python pandas實(shí)現(xiàn)excel工作表合并功能詳解

    Python pandas實(shí)現(xiàn)excel工作表合并功能詳解

    這篇文章主要介紹了Python pandas實(shí)現(xiàn)excel工作表合并功能以及相關(guān)實(shí)例代碼,需要的朋友們參考學(xué)習(xí)下。
    2019-08-08
  • Python靜態(tài)類(lèi)型檢查新工具之pyright 使用指南

    Python靜態(tài)類(lèi)型檢查新工具之pyright 使用指南

    這篇文章主要介紹了Python靜態(tài)類(lèi)型檢查新工具之pyright 使用指南,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • Python設(shè)計(jì)模式之抽象工廠(chǎng)模式

    Python設(shè)計(jì)模式之抽象工廠(chǎng)模式

    這篇文章主要為大家詳細(xì)介紹了Python設(shè)計(jì)模式之抽象工廠(chǎng)模式,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 詳解pandas獲取Dataframe元素值的幾種方法

    詳解pandas獲取Dataframe元素值的幾種方法

    這篇文章主要介紹了詳解pandas獲取Dataframe元素值的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 如何使用conda和pip批量安裝Python包

    如何使用conda和pip批量安裝Python包

    這篇文章主要介紹了如何使用conda和pip批量安裝Python包問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python計(jì)算時(shí)間間隔(精確到微妙)的代碼實(shí)例

    Python計(jì)算時(shí)間間隔(精確到微妙)的代碼實(shí)例

    今天小編就為大家分享一篇關(guān)于Python計(jì)算時(shí)間間隔(精確到微妙)的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02

最新評(píng)論