[python]クローラーノート(10)セレンアクション

  • ウェブサイトで動的荷重データを便利に取得
  • シミュレートされたログインを便利に実装する

セレン

  • ブラウザの自動化に基づくモジュール
  • pipインストールセレン
  • Google Chromeドライバーをダウンロードします:http://npm.taobao.org/mirrors/chromedriver/87.0.4280.88/
    http://chromedriver.storage.googleapis.com/index.html(壁を覆すため)
  • Google Chromeでの表示方法:Google Chromeを開き、アドレスバーにchrome:// version /と入力します
  • セレンを介してブラウザオブジェクトをインスタンス化します。
from selenium import webdriver
#实例化一个浏览器对象(传入浏览器的驱动程序)
bro = webdriver.Chrome(executable_path='./chromedriver.exe')
url = "https://www.bilibili.com/video/BV1ha4y1H7sx?p=50"
#让浏览器发起指定url对应的请求
def play(bro,url):
    bro.get(url=url)

#获取浏览器当前源码数据
play(bro,url)

淘宝網自動検索:

from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver.exe')

bro.get('https://www.taobao.com/')

#标签定位
search_input = bro.find_element_by_id('q')
#标签交互
search_input.send_keys('Iphone')

#点击搜索按钮
button = bro.find_element_by_class_name('btn-search')
button.click()#点击搜索按钮

sleep(5)
bro.quit()

1.リクエストを開始しますget(url)
2。タグの配置:一連のメソッドを検索します
3.タグの相互作用:send_keys(str)
4。jsプログラムを実行します:excute_script( 'jscode')
5。前方および後方:後方前方
6.閉じるブラウザ:終了

ターゲットラベルがiframeラベルにある場合は、次の操作を実行してラベルを見つける必要があります。

bro.switch_to.frame('iframeResult')#切换浏览器标签的作用域

アクションチェーン:

from selenium.webdriver import ActionChains

action = ActionChains(bro)
action.click_and_hold(div)#长按
偏移
action.move_by_offset(17,0).perform()#perform立即执行动作链动作
action.release()#释放动作链

おすすめ

転載: blog.csdn.net/Sgmple/article/details/112187561