09.python之网络爬虫之selenium、phantomJs和谷歌无头浏览器的自动化操作

09.python之网络爬虫之selenium、phantomJs和谷歌无头浏览器的自动化操作

一、selenium

  • 什么是selenium?
    • 是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作。  

  • 环境搭建

    • 安装selenum:pip install selenium

    • 获取某一款浏览器的驱动程序(以谷歌浏览器为例)

      • 谷歌浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html

      • 下载的驱动程序必须和浏览器的版本统一,大家可以根据http://blog.csdn.net/huilan_same/article/details/51896672中提供的版本映射表进行对应

          效果展示:

  from selenium import webdriver
  from time import sleep

  # 创建浏览器对象,后面是你的浏览器驱动位置,记得前面加r'','r'是防止字符转义的
  bai = webdriver.Chrome(executable_path=r'D:pycharmexercise13.爬虫day4谷歌浏览器驱动chromedriver.exe')

  # 同过get发送请求
  bai.get(url='https://www.baidu.com')

  # 根据find系列的函数定位到指定的标签
  my_input = bai.find_element_by_id("kw")
  sleep(3)
  # 向标签中指定录入的内容
  my_input.send_keys("美女")
  sleep(3)
  # 根据find系列的函数定位到指定的标签
  my_button = bai.find_element_by_id("su").click()
  sleep(3)
  # 获取当前浏览器显示的页面的页面源码
  page_text = bai.page_source
  #关闭浏览器
  bai.quit()

 代码解释:

#导包
from selenium import webdriver  
#创建浏览器对象,通过该对象可以操作浏览器
browser = webdriver.Chrome('驱动路径')
#使用浏览器发起指定请求
browser.get(url)

#使用下面的方法,查找指定的元素进行操作即可
    find_element_by_id            根据id找节点
    find_elements_by_name         根据name找
    find_elements_by_xpath        根据xpath查找
    find_elements_by_tag_name     根据标签名找
    find_elements_by_class_name   根据class名字查找

#像指定的标签元素中录入指定的内容
   send_keys("指定输入的内容")

page_source 获取当前浏览器显示的页面的页面源码
quit() 关闭浏览器

  示例:自动化登陆qq空间,并获取部分好友动态信息 

   
 1 from selenium import webdriver
 2 from time import sleep
 3 from lxml import etree
 4 
 5 # 爬取qq空间内容
 6 bro= webdriver.Chrome(executable_path=r'D:pycharmexercise13.爬虫day4谷歌浏览器驱动chromedriver.exe')
 7 
 8 url = 'https://qzone.qq.com/'
 9 
10 bro.get(url=url)
11 sleep(1)
12 
13 # 定位到指定的iframe,  switch_to.frame() 定位到当前指定元素
14 bro.switch_to.frame('login_frame')
15 bro.find_element_by_id('switcher_plogin').click()
16 sleep(1)
17 
18 username = bro.find_element_by_id("u")
19 username.send_keys('169675573')
20 password = bro.find_element_by_id("p")
21 password.send_keys('xxxxxx')
22 sleep(1)
23 bro.find_element_by_id('login_button').click()
24 sleep(2)
25 
26 # 拿到登陆之后的页面
27 page_text = bro.page_source
28 sleep(5)
29 
30 
31 tree = etree.HTML(page_text)
32 div_list = tree.xpath('//div[@class="f-info qz_info_cut"] | div[@class="f-info"]')
33 for div in div_list:
34     text = div.xpath('./text(text)')
35     text = "".join()
36     print(text)
37 print("下载完成!!!")
38 bro.quit()
实现代码

二、 phantomJs

  • PhantomJS是一款*面的浏览器,其自动化操作流程和上述操作谷歌浏览器是一致的。
  • 由于是*面的,为了能够展示自动化操作流程,PhantomJS为用户提供了一个截屏的功能,使用save_screenshot函数实现。

  效果展示:

  # *面浏览器的使用
  from selenium import webdriver
  from time import sleep

  #创建浏览器对象,通过该对象可以操作浏览器,后面是你的浏览器驱动位置,记得前面加r'','r'是防止字符转义的
  bai = webdriver.PhantomJS(executable_path=r'D:pycharmexercise13.爬虫day4phantomjs-2.1.1-windowsinphantomjs.exe')

  # 同过get发送请求
  bai.get(url='https://www.baidu.com')
  bai.save_screenshot('./1.jpg')  # 截图    在*面执行的时候截取当前执行动作
  # 根据find系列的函数定位到指定的标签
  my_input = bai.find_element_by_id("kw")
  sleep(3)
  # send_keys 向标签中指定录入的内容
  my_input.send_keys("美女")
  sleep(3)
  # 根据find系列的函数定位到指定的标签
  my_button = bai.find_element_by_id("su").click()
  sleep(3)
  # 获取当前浏览器显示的页面的页面源码
  page_text = bai.page_source
  bai.save_screenshot('./2.jpg')  # save_screenshot函数实现截图功能
  print(page_text)

  # 关闭浏览器
  bai.quit() 

  selenium 和 phantomJs 实现的功能是一样的,都需要指定驱动程序,

  不同的:

     selenium:是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作。

       phantomJs:是一款*面的浏览器,其自动化操作流程和上述操作谷歌浏览器是一致的

三、谷歌无头浏览器

  由于PhantomJs最近已经停止了更新和维护,所以推荐大家可以使用谷歌的无头浏览器,是一款*面的谷歌浏览器

  在使用谷歌无头浏览器的时候,需要导入一个包,并创建参数对象,并在创建浏览器对象的时候,将创建的参数对象,添加到浏览器驱动位置的后面,即可使用谷歌无头浏览器

    导入包:

    from selenium.webdriver.chrome.options import Options      

     创建一个参数对象,用来控制chrome以*面模式打开:
      chrome_options = Options()
      chrome_options.add_argument('--headless')
      chrome_options.add_argument('--disable-gpu')

    将创建的参数对象添加到浏览器对象的驱动位置后面:

     webdriver.Chrome('驱动路径', chrome_options=chrome_options)

  效果展示:

  # 谷歌无头浏览器
  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')

  bai = webdriver.Chrome(executable_path= r'D:pycharmexercise13.爬虫day4谷歌浏览器驱动chromedriver.exe',chrome_options=chrome_options)

  # 同过get发送请求
  bai.get(url='https://www.baidu.com')

  # 根据find系列的函数定位到指定的标签
  my_input = bai.find_element_by_id("kw")
  sleep(3)
  # 向标签中指定录入的内容
  my_input.send_keys("美女")
  sleep(3)
  # 根据find系列的函数定位到指定的标签
  my_button = bai.find_element_by_id("su").click()
  sleep(3)
  # 获取当前浏览器显示的页面的页面源码
  page_text = bai.page_source
  print(page_text)

  # 关闭浏览器
  bai.quit()

 selenium+phantomjs 就是爬虫终极解决方案:

   有些网站上的内容信息是通过动态加载js形成的,所以使用普通爬虫程序无法回去动态加载的js内容。例如豆瓣电影中的电影信息是通过下拉操作动态加载更多的电影信息。

  示例:需求是尽可能多的爬取豆瓣网中的电影信息

  from selenium import webdriver
  from time import sleep

  bro= webdriver.Chrome(executable_path=r'D:pycharmexercise13.爬虫day4谷歌浏览器驱动chromedriver.exe')
  url = 'https://movie.douban.com/typerank?type_name=%E7%88%B1%E6%83%85&type=13&interval_id=100:90&action='

  # 请求发送
  bro.get(url=url)

  # 获取当前网页页面显示的高度
  js = "window.scrollTo(0,document.body.scrollHeight)"
  # 执行js代码,execute_script该函数可以执行一组字符串形式的js代码
  bro.execute_script(js)
  sleep(2)
  bro.execute_script(js)
  sleep(2)
  bro.execute_script(js)
  sleep(2)
  bro.execute_script(js)
  sleep(2)
  page_text = bro.page_source # page_source 该属性可以获取当前浏览器的当前页的源码(html) 

  with open("./douban.html","w",encoding="utf-8") as fp:
      fp.write(page_text)

  bro.quit()