1. 程式人生 > 實用技巧 >爬蟲selenium庫學習

爬蟲selenium庫學習

爬蟲 selenium

selenium是一個基於瀏覽器自動化的模組,可以用於12306搶票

使用這個庫需要先下載一個瀏覽器驅動程式

//獲取動態載入的資料
from selenium import webdriver
from lxml import etree
from time import sleep
# 例項化一個瀏覽器物件
bro = webdriver.Chrome(executable_path = './chromedriver')
bro.get('http://scxk.nmpa.gov.cn:81/xk/')
page_text = bro.page_source
tree = etree.HTML(page_text)
li_list = tree.xpath('//ul[@id="gzlist"]/li')
for li in li_list:
	name = li.xpath('./dl/@title')[0]
	print(name)
sleep(15)
bro.quit()
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path = './chromedriver')
bro.get('https://www.taobao.com/')
# 標籤定位
search_input = bro.find_element_by_id('q')
# 標籤互動
search_input.send_keys('Iphone')
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
# 點選搜尋
btn = bro.find_element_by_css_selector('.btn-search')
btn.click()
bro.get('https://www.baidu.com/')
sleep(3)
bro.back()
sleep(3)
bro.forward()