zoukankan      html  css  js  c++  java
  • 基于Python, Selenium, Phantomjs无头浏览器访问页面

    引言: 在自动化测试以及爬虫领域,无头浏览器的应用场景非常广泛,本文将梳理其中的若干概念和思路,并基于代码示例其中的若干使用技巧。

    1. 无头浏览器

    通常大家在在打开网页的工具就是浏览器,通过界面上输入网址就可以访问相应的站点内容,这个就是通常所说的基于界面的浏览器。除了这种浏览器之外,还有一种叫做无头浏览器的东西,主要是用作爬虫,用以捕捉Web上的各类数据;这里的无头主要是指没有界面,完全是后台操作,对于网站来说,它以为访问它的就是一个真实的浏览器。

     此类的框架包括: Phantomjs为代表,其它还有非常繁多的无头浏览器,大家可以自行了解一下。

    2. Phantomjs

      以javascript实现的一个无头浏览器,兼容大多数的浏览器标准,本质上是一个javascript的执行引擎和解析器。通过都是以它为底层服务,然后开发第三方其它语言的适配模块,从而打通访问phantomjs的通道, 比如Selenium, ghostdriver.

     其官方站点为: http://phantomjs.org,其支持多个平台的使用和部署。

    3.  Selenium

       其为Web的自动化测试框架,实现了WebDriver的接口,提供了不同平台操作各类浏览器的接口,比如目前主流的: IE, Firefox, Chrome, Opera, Android等各个平台的访问。

    其起步阶段目标是满足自动化的需求,但其由于起特性,也可以用于页面的浏览访问,比如基于无头浏览器的数据抓取和捕获。

     Selenium提供了多种语言的接口和多个平台/浏览器的支持,常见的有Java, Python, Javascript, Ruby等。

     官方站点为:https://github.com/SeleniumHQ/selenium

    4.  ghostdriver

      根据其官方的描述:Ghost Driver is a pure JavaScript implementation of the WebDriver Wire Protocol for PhantomJS. It's a Remote WebDriver that uses PhantomJS as back-end.

     其就是一个简要的WebDriver的实现,基于Javascript语言来实现,方便基于PhantomJS作为后端来通信。

     官方地址: https://github.com/detro/ghostdriver

    5.  WebDriver

        WebDriver是由W3C协会制定的用以描述浏览器行为的一组标准接口,Selenium实现其中部分的接口,大部分的浏览器都是以该标准来作为衡量优劣和完善与否的标准。

     W3C的web driver定义: https://www.w3.org/TR/webdriver/

    6.  代码示例

     让我们通过一段代码来看看如何基于Selenium和PhantomJS来实现自动化访问页面吧:

    #from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
    from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
    from selenium.webdriver.phantomjs.webdriver import WebDriver

    # Create a new instance of the Firefox driver
    driver = WebDriver(executable_path='/opt/phantomjs-2.1.1-linux-x86_64/bin/phantomjs', port=5001)

    # go to the google home page
    driver.get("http://www.baidu.com")

    # the page is ajaxy so the title is originally this:
    print(driver.title)

    # find the element that's name attribute is q (the google search box)
    inputElement = driver.find_element_by_id("kw")

    # type in the search
    inputElement.send_keys("cheese!")

    # submit the form (although google automatically searches now without submitting)
    inputElement.submit()

    try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))

    # You should see "cheese! - Google Search"
    print(driver.title)
    print(driver.get_cookies())

    finally:
    driver.quit()
      这里基于PhantomJS作为无头浏览器的实现,WebDriver中的executable_path是放置PhantomJS的路径。这里在页面打开之后,输出了title,动态输入了cheese关键词,然后点击回车,最后打出了cookies信息。
    7.   API相关信息

      Selenium Doc: https://seleniumhq.github.io/selenium/docs/api/py/index.html

           Selenium API: https://seleniumhq.github.io/selenium/docs/api/py/api.html

           Web Driver API: http://selenium-python.readthedocs.io/api.html

    8. 总结

     这里有一个概念需要澄清一下, Selenium原始的初衷是做基于浏览器的自动化测试,所以其大部分的功能都是在基于浏览器的访问和接口操作,操作的都是有界面的浏览器;PhantomJS只是其中无界面的浏览器的一个实现而已了。对于不同的WebDriver接口的使用遵循上述的原则。

  • 相关阅读:
    shell入门-sed-2替换功能
    shell入门-sed-1
    shell入门-grep-3-egrep
    shell入门-grep2
    shell入门-grep过滤-1
    shell入门-连接符(并且、和、或者)
    shell入门-tr替换字符和split切割大文件
    shell入门-uniq去重复和tee重定向
    class类的相关操作 --| 公有普通方法 | 私有普通方法 | 无参方法 | 有参方法
    类的封装性-- | 成员属性 | 成员方法 | 私有属性 | 私有方法 之间调用
  • 原文地址:https://www.cnblogs.com/zknublx/p/9837796.html
Copyright © 2011-2022 走看看