selenium模块是基于浏览器自动化的一个模块。
注:win32版本可兼容64
下载完驱动后,将.exe文件放入与.py文件同一个文件夹下
from selenium import webdriver
from lxml import etree
from time import sleep
#实例化一个浏览器对象(传入浏览器的驱动程序)
bro = webdriver.Chrome(executable_path='./chromedriver')
#让浏览器发起一个指定url对应请求
bro.get('https://www.gushiwen.org/gushi/tangshi.aspx')
#获取浏览器当前页面的页面源码数据
page_text = bro.page_source
#解析企业名称
tree = etree.HTML(page_text)
span_list = tree.xpath('//div[@class="sons"]/div[1]/span')
for span in span_list:
name = span.xpath('./a/text()')[0]
print(name)
sleep(5)
bro.quit()
from selenium import webdriver
from lxml import etree
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')
#点击搜索按钮
btn = bro.find_element_by_css_selector('.btn-search')
btn.click()
sleep(5)
bro.close()
from selenium import webdriver
from lxml import etree
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://www.taobao.com/')
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(5)
bro.quit()
from selenium import webdriver
from lxml import etree
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://www.taobao.com/')
bro.get('https://www.baidu.com')
# 回退
bro.back()
sleep(2)
# 前进
bro.forward()
sleep(2)
bro.close()
如果定位的标签存在于iframe标签中,则必须使用switch_to.frame(id)
动作链(拖动):
from selenium.webdriver import ActionChains
代码
from selenium import webdriver
from time import sleep
#导入动作链对应的类
from selenium.webdriver import ActionChains
bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
#如果定位的标签是存在于iframe标签之中的则必须通过如下操作在进行标签定位
bro.switch_to.frame('iframeResult')#切换浏览器标签定位的作用域
div = bro.find_element_by_id('draggable')
#动作链
action = ActionChains(bro)
#点击长按指定的标签
action.click_and_hold(div)
for i in range(5):
#perform()立即执行动作链操作
#move_by_offset(x,y):x水平方向 y竖直方向
action.move_by_offset(17,0).perform()
sleep(0.5)
#释放动作链
action.release()
bro.quit()
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://qzone.qq.com/')
bro.switch_to.frame('login_frame')
a_tag = bro.find_element_by_id("switcher_plogin")
a_tag.click()
userName_tag = bro.find_element_by_id('u')
passName_tag = bro.find_element_by_id('p')
sleep(1)
userName_tag.send_keys('123456789')
sleep(1)
passName_tag.send_keys('987654321')
sleep(1)
btn = bro.find_element_by_id('login_button')
btn.click()
sleep(1)
bro.close()
需求:实现使用selenium时无可视化界面并且可以规避检测(反反爬虫机制)
代码
from selenium import webdriver
from time import sleep
# 实现无可视化界面
from selenium.webdriver.chrome.options import Options
# 实现规避检测
from selenium.webdriver import ChromeOptions
#实现无可视化界面的操作
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 实现规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
bro = webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options,options=option)
#无可视化界面(无头浏览器) phantomJs
bro.get('https://www.baidu.com')
print(bro.page_source)
sleep(2)
bro.close()