[python] Crawler notes (ten) selenium action

  • Conveniently obtain dynamic loading data in the website
  • Conveniently implement simulated login

selenium

  • A module based on browser automation
  • pip install selenium
  • Download a Google Chrome driver: http://npm.taobao.org/mirrors/chromedriver/87.0.4280.88/
    http://chromedriver.storage.googleapis.com/index.html (to overturn the wall)
  • Viewing method in Google Chrome: Open Google Chrome and enter chrome://version/ in the address bar
  • Instantiate a browser object through selenium:
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)

Taobao automatic search:

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. Initiate the request get(url)
2. Tag positioning: find series of methods
3. Tag interaction: send_keys(str)
4. Execute js program: excute_script('jscode')
5. Forward and backward: back forward
6. Close the browser : Quit

If the target label is located in the iframe label, you must perform the following operations to locate the label:

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

Action chain:

from selenium.webdriver import ActionChains

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

Guess you like

Origin blog.csdn.net/Sgmple/article/details/112187561