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()
原文:https://www.cnblogs.com/serendipity-my/p/13681607.html