七、selenium
7.1、简介
基于浏览器自动化的一个模块。
7.1.1、特点
便捷的获取网站中动态加载的数据。
便捷实现模拟登录。
7.2、环境的安装
pip install se Lenium
下载浏览器的驱动程序
三大浏览器的驱动地址:
chrome驱动:
http://chromedriver.storage.googleapis.com/index.html
Firefox驱动:
https://github.com/mozilla/geckodriver/releases/
edge驱动:
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
7.3、常用方法
● 发起请求: get(url)
● 标签定位: find系列的方法(有提示)
● 标签交互: send_keys(‘xxx’)
● 执行js程序: excute_script(‘js代码’)
● 前进,后退: back(),forward()
● 关闭浏览器: quit()
7.4、selenium处理iframet
如果定位的标签是存在于iframe之中的则必须通过如下提作在进行新置定位,通过iframe的id定位 切换浏览器标签定位的作用域
bro.switch_to.frame(id)
动作链的使用:
● 实例化一个动作链对象:action = Act ionChains (bro)
● click_and_hold(div):长按且点击操作
● move_by_offset(x,y):移动
● perform():让动作链立即执行
● action.release():释放动作链
7.5、无头浏览器(后台运行)
注意:phantomjs是一个无头浏览器
谷歌无头模式设置代码:
from selenium import webdriver
# from selenium.webdriver.firefox.options import Options
# from selenium.webdriver.edge.options import Options
# 实现无可视化界面
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")
# 实现让selenium规避被检测到的风险
option = ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
# 实例化一个浏览器对象(传入浏览器的驱动成)。
bro = webdriver.Chrome(executable_path="./chromedriver.exe",
chrome_options=chrome_options,
options=option
)
bro.get("http://qzone.qq.com")
print(bro.page_source)
bro.quit()
7.6、selenium规避被检测识别
现在不少大网站有对selenium采取了监测机制。比如正常情况下我们用浏览器访问淘宝等网站的window.navigator.webdriver值为undefined,而使用selenium访问则该值为true,那么如何解决这个问题呢?
只需要设置Chromedriver的启动参数即可解决问题。在启动Chromedriver之前,为Chrome开启实验性功能参数 excludeSwitches。 它的值为 [“enable-automation”]。
from selenium import webdriver
# from selenium.webdriver.firefox.options import Options
# from selenium.webdriver.edge.options import Options
# 实现无可视化界面
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")
# 实现让selenium规避被检测到的风险
option = ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
# 实例化一个浏览器对象(传入浏览器的驱动成)。
bro = webdriver.Chrome(executable_path="./chromedriver.exe",
chrome_options=chrome_options,
options=option
)
bro.get("http://qzone.qq.com")
print(bro.page_source)
bro.quit()
7.7、案例
获取药监局列表信息
from lxml import etree
from selenium import webdriver
# 实例化一个浏览器对象(传入浏览器的驱动成)。
bro = webdriver.Edge(executable_path="./msedgedriver.exe")
# 让浏览器发起指定url对应请求
bro.get("http://scxk.nmpa.gov.cn:81/xk/")
# 获取浏览器当前页面的页面源码数据
page_text = bro.page_source
tree = etree.HTML(page_text)
li_list = tree.xpath("//*[@id='gzlist']/li")
for li in li_list:
name = li.xpath("./dl/@title")[0]
print(name)
bro.quit()
模拟12306登录:
分析:
● 使用selenium打开登录页面。
● 对当前selenium打开的这张页面进行截图。save_screenshot()
● 对当前图片局部区域(验证码图片)进行裁剪。
● 好处:将验证码图片和模拟登录进行一一对应。
import time
import requests
from lxml import etree
from selenium import webdriver
# from selenium.webdriver.firefox.options import Options
# from selenium.webdriver.edge.options import Options
# 实现无可视化界面
from selenium.webdriver.chrome.options import Options
# 实现规避检测
from selenium.webdriver import ChromeOptions
# 模拟12306登录
# 实现无可视化界面的操作
chrome_options = Options()
# chrome_options.add_argument("--headless")
# chrome_options.add_argument("--disable-gpu")
# 实现让selenium规避被检测到的风险
option = ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
# 实例化一个浏览器对象(传入浏览器的驱动成)。
bro = webdriver.Chrome(executable_path="./chromedriver.exe",
chrome_options=chrome_options,
options=option
)
bro.maximize_window()
bro.get("https://kyfw.12306.cn/otn/resources/login.html")
login_btn = bro.find_element_by_xpath("/html/body/div[2]/div[2]/ul/li[2]/a")
login_btn.click()
bro.save_screenshot("./ccc.png")
page = bro.page_source
tree = etree.HTML(page)
img_src = tree.xpath('//*[@id="J-loginImg"]/@src')[0]
bro.find_element_by_id("J-loginImg")
requests.get(url=img_src,)
time.sleep(2)
# bro.quit()
操作浏览器打开淘宝等一系列简单操作
import time
from selenium import webdriver
bro = webdriver.Edge('./msedgedriver.exe')
bro.get("https://www.taobao.com")
# 标签定位
search_input = bro.find_element_by_id("q")
# 标签交互
search_input.send_keys("iphone")
time.sleep(2)
# 执行一组JS代码
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
# 点击搜索按钮
time.sleep(2)
btn = bro.find_element_by_xpath(xpath='//*[@id="J_TSearchForm"]/div[1]/button')
btn.click()
# 继续访问百度首页
bro.get("https:www.baidu.com")
time.sleep(2)
# 后退
bro.back()
time.sleep(2)
# 前进
bro.forward()
time.sleep(2)
bro.quit()
模拟登陆:
import time
from selenium import webdriver
# 实例化一个浏览器对象(传入浏览器的驱动成)。
bro = webdriver.Edge(executable_path="./msedgedriver.exe")
# 让浏览器发起指定url对应请求
bro.get("http://qzone.qq.com")
bro.switch_to.frame("login_frame")
btn = bro.find_element_by_id("switcher_plogin")
btn.click()
username = bro.find_element_by_id("u")
password = bro.find_element_by_id("p")
img_btn = bro.find_element_by_id("slideBlock")
log_btn = bro.find_element_by_id('login_button')
username.send_keys(".............")
password.send_keys("............")
log_btn.click()
time.sleep(5)
bro.quit()