爬虫之Selenium

简介

selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器

作用: 可以让浏览器完成相关自动化的操作

和爬虫的关联:

  • 模拟登陆
  • 可以获取动态加载的页面数据

编码流程:

  • 导包
  • 实例化浏览器对象(驱动)
  • 制定相关自动化的行为动作

环境安装

  • 下载安装selenium:pip install selenium
  • 下载浏览器驱动程序:
    • http://chromedriver.storage.googleapis.com/index.html
  • 查看驱动和浏览器版本的映射关系:
    • http://blog.csdn.net/huilan_same/article/details/51896672

简单使用/效果展示

01:

复制代码
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver.exe')  
bro.get('https://www.baidu.com')    #获取的连接页面
sleep(2)
#标签定位
tag_input = bro.find_element_by_id('kw')
tag_input.send_keys('人民币')   #标签中输入值
sleep(2)

btn = bro.find_element_by_id('su')   
btn.click()  #标签点击事件
sleep(2)

bro.quit()  #退出
复制代码

02.

复制代码
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver.exe')

bro.get('https://xueqiu.com/')
sleep(5)

#执行js实现滚轮向下滑动
js = 'window.scrollTo(0,document.body.scrollHeight)'    #两个参数一个是X轴,一个是y轴,此时用的是Y轴
bro.execute_script(js)
sleep(2)
bro.execute_script(js)
sleep(2)
bro.execute_script(js)
sleep(2)
bro.execute_script(js)
sleep(2)

a_tag = bro.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[1]/div[2]/div[2]/a')
a_tag.click()
sleep(5)
#获取当前浏览器页面数据(动态)
print(bro.page_source)    

bro.quit()
复制代码

PhantomJs及谷歌无头浏览器无可视化:

#PhantomJs是一款无可视化界面的浏览器(免安装)
from selenium import webdriver
from time import sleep
bro = webdriver.PhantomJS(executable_path=r'C:\Users\Administrator\Desktop\爬虫+数据\爬虫day03\phantomjs-2.1.1-windows\bin\phantomjs.exe')

bro.get('https://xueqiu.com/')
sleep(2)
bro.save_screenshot('./1.png')
#执行js实现滚轮向下滑动
js = 'window.scrollTo(0,document.body.scrollHeight)'
bro.execute_script(js)
sleep(2)
bro.execute_script(js)
sleep(2)
bro.execute_script(js)
sleep(2)
bro.execute_script(js)
sleep(2)
bro.save_screenshot('./2.png')
# a_tag = bro.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[1]/div[2]/div[2]/a')
# bro.save_screenshot('./2.png')
# a_tag.click()
sleep(2)
#获取当前浏览器页面数据(动态)
print(bro.page_source)

bro.quit()

现在用的很少,知道即可

  

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
# 创建一个参数对象,用来控制chrome以无界面模式打开
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

bro = webdriver.Chrome(executable_path='./chromedriver.exe',options=chrome_options)
bro.get('https://www.baidu.com')
sleep(2)
bro.save_screenshot('1.png')
#标签定位
tag_input = bro.find_element_by_id('kw')
tag_input.send_keys('人民币')
sleep(2)

btn = bro.find_element_by_id('su')
btn.click()
sleep(2)

print(bro.page_source)
bro.quit()

谷歌无头浏览器

  

前进和后退

#前进和后退
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='./chromedriver.exe')
bro.get('https://www.baidu.com')
sleep(1)
bro.get('http://www.goubanjia.com/')
sleep(1)
bro.get('https://www.taobao.com')
sleep(1)

bro.back()
sleep(1)
bro.forward()
sleep(1)
print(bro.page_source)

bro.quit()

  

动作链

在上面的实例中,一些交互动作都是针对某个节点执行的。比如,对于输入框,我们就调用它的输入文字和清空文字方法;对于按钮,就调用它的点击方法。其实,还有另外一些操作,它们没有特定的执行对象,比如鼠标拖曳、键盘按键等,这些动作用另一种方式来执行,那就是动作链。

比如,现在实现一个节点的拖曳操作,将某个节点从一处拖曳到另外一处,可以这样实现:

#动作链
from selenium import webdriver
from time import sleep
from selenium.webdriver import ChromeOptions
from selenium.webdriver import ActionChains  

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])

bro = webdriver.Chrome(executable_path='./chromedriver.exe',options=option)
url = 'https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
bro.get(url=url)
#如果定位的标签存在于iframe标签之中,则必须经过switch_to操作在进行标签定位
bro.switch_to.frame('iframeResult')
source_tag = bro.find_element_by_id('draggable')
taget_tag = bro.find_element_by_id('droppable')
#创建一个动作连的对象
action = ActionChains(bro)
action.drag_and_drop(source_tag,taget_tag)
action.perform()
sleep(3)
# bro.quit()

  

浏览器创建

Selenium支持非常多的浏览器,如Chrome、Firefox、Edge等,还有Android、BlackBerry等手机端的浏览器。另外,也支持无界面浏览器PhantomJS。

复制代码
from selenium import webdriver
  
browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.PhantomJS()
browser = webdriver.Safari()
复制代码

元素定位

webdriver 提供了一系列的元素定位方法,常用的有以下几种:

复制代码
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
复制代码

注意

1、find_element_by_xxx找的是第一个符合条件的标签,find_elements_by_xxx找的是所有符合条件的标签。

2、根据ID、CSS选择器和XPath获取,它们返回的结果完全一致。

3、另外,Selenium还提供了通用方法find_element(),它需要传入两个参数:查找方式By和值。实际上,它就是find_element_by_id()这种方法的通用函数版本,比如find_element_by_id(id)就等价于find_element(By.ID, id),二者得到的结果完全一致。

节点交互

Selenium可以驱动浏览器来执行一些操作,也就是说可以让浏览器模拟执行一些动作。比较常见的用法有:输入文字时用send_keys()方法,清空文字时用clear()方法,点击按钮时用click()方法。示例如下:

复制代码
from selenium import webdriver
import time
 
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input = browser.find_element_by_id('q')
input.send_keys('MAC')
time.sleep(1)
input.clear()
input.send_keys('IPhone')
button = browser.find_element_by_class_name('btn-search')
button.click()
browser.quit()
复制代码

selenium规避被检测识别

现在不少大网站有对selenium采取了监测机制。比如正常情况下我们用浏览器访问淘宝等网站的 window.navigator.webdriver的值为 
undefined。而使用selenium访问则该值为true。那么如何解决这个问题呢?

只需要设置Chromedriver的启动参数即可解决问题。在启动Chromedriver之前,为Chrome开启实验性功能参数excludeSwitches,它的值为['enable-automation'],完整代码如下:

from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = Chrome(options=option)

猜你喜欢

转载自www.cnblogs.com/xinjie123/p/10848793.html