基于Python, Selenium, Phantomjs无头浏览器访问页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了基于Python, Selenium, Phantomjs无头浏览器访问页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3731字,纯文字阅读大概需要6分钟。
内容图文
![基于Python, Selenium, Phantomjs无头浏览器访问页面](/upload/InfoBanner/zyjiaocheng/850/2e2c27d97ac84e199ece4268a3528d21.jpg)
引言: 在自动化测试以及爬虫领域,无头浏览器的应用场景非常广泛,本文将梳理其中的若干概念和思路,并基于代码示例其中的若干使用技巧。
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接口的使用遵循上述的原则。
内容总结
以上是互联网集市为您收集整理的基于Python, Selenium, Phantomjs无头浏览器访问页面全部内容,希望文章能够帮你解决基于Python, Selenium, Phantomjs无头浏览器访问页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。