selenium+phantomjs解析JS
背景知识:
PhantomJS 是一个基于WebKit的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。PhantomJS可以用于页面自动化,网络监测,网页截屏,以及无界面测试等。
Selenium也是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7、8、9)、Mozilla Firefox、Mozilla Suite等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。
PhantomJS 用来渲染解析JS,Selenium 用来驱动以及与 Pyt

#coding=utf-8
from selenium import webdriver driver = webdriver.PhantomJS(executable_path=‘C:UsersGentlyguitarDesktopphantomjs-1.9.7-windowsphantomjs.exe‘)
driver.get("http://phperz.com/")
driver.find_element_by_id(‘search_form_input_homepage‘).send_keys("Nirvana")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()

hon 的对接,Python 进行后期的处理。
selenium2支持的Python版本:2.7, 3.2, 3.3 and 3.4
如果需要进行远程操作的话,就需要额外安装selenium server
安装:
先装selenium2,哪种方式装都可以,我一般都是直接下载压缩包,然后用python setup.py install命令来装,selenium 2.42.1的下载地址:https://pypi.python.org/pypi/selenium/2.42.1
然后下载phantomjs,https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-windows.zip,解压后可以看到一个phantomjs.exe的文件
范例1:
其中的executable_path就是刚才phantomjs.exe的路径,运行结果:
https://phperz.com/?q=Nirvana
Walk through of the example:
值得一提的是:
get方法会一直等到页面被完全加载,然后才会继续程序
但 是对于ajax: It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded
send_keys就是填充input
范例2:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
import sys driver = webdriver.PhantomJS(executable_path=‘C:UsersGentlyguitarDesktopphantomjs-1.9.7-windowsphantomjs.exe‘)
driver.get("http://www.zhihu.com/#signin")
#driver.find_element_by_name(‘email‘).send_keys(‘your email‘)
driver.find_element_by_xpath(‘//input[@name="password"]‘).send_keys(‘your password‘)
#driver.find_element_by_xpath(‘//input[@name="password"]‘).send_keys(Keys.RETURN)
time.sleep(2)
driver.get_screenshot_as_file(‘show.png‘)
#driver.find_element_by_xpath(‘//button[@class="sign-button"]‘).click()
driver.find_element_by_xpath(‘//form[@class="zu-side-login-box"]‘).submit() try:
dr=WebDriverWait(driver,5)
dr.until(lambda the_driver:the_driver.find_element_by_xpath(‘//a[@class="zu-top-nav-userinfo "]‘).is_displayed())
except:
print ‘登录失败‘
sys.exit(0)
driver.get_screenshot_as_file(‘show.png‘)
#user=driver.find_element_by_class_name(‘zu-top-nav-userinfo ‘)
#webdriver.ActionChains(driver).move_to_element(user).perform() #移动鼠标到我的用户名
loadmore=driver.find_element_by_xpath(‘//a[@id="zh-load-more"]‘)
actions = ActionChains(driver)
actions.move_to_element(loadmore)
actions.click(loadmore)
actions.perform()
time.sleep(2)
driver.get_screenshot_as_file(‘show.png‘)
print driver.current_url
print driver.page_source
driver.quit()

这个程序完成的是,登陆知乎,然后能自动点击页面下方的“更多”,以载入更多的内容
Walk through of the example:
from selenium.webdriver.common.keys import Keys,keys这个类就是键盘上的键,文中的send_keys(Keys.RETURN)就是按一个回车
from selenium.webdriver.support.ui import WebDriverWait是为了后面一个等待的操作
from selenium.webdriver import ActionChains是导入一个动作的类,这句话的写法,我找了很久
find_element推荐使用Xpath的方法,非常方便
Xpath表达式写法教程:http://www.ruanyifeng.com/blog/2009/07/xpath_path_expressions.html
值得注意的是,避免选择value带有空格的属性,譬如class = "country name"这种,不然会报错,大概compound class之类的错
检查用户密码是否输入正确的方法就是在填入后截屏看看
想要截屏,这么一句话就行:
driver.get_screenshot_as_file(‘show.png‘)
但是,这里的截屏是不带滚动条的,就是给你把整个页面全部照下来
try:
dr=WebDriverWait(driver,5)
dr.until(lambda the_driver:the_driver.find_element_by_xpath(‘//a[@class="zu-top-nav-userinfo "]‘).is_displayed())
except:
print ‘登录失败‘
sys.exit(0)
是用来通过检查某个元素是否被加载来检查是否登录成功,我认为当个黑盒子用就可以了。其中5的解释:5秒内每隔500毫秒扫描1次页面变化,直到指定的元素
对于表单的提交,即可以选择登录按钮然后使用click方法,也可以选择表单然后使用submit方法,后者能应付没有登录按钮的情况,所以推荐使用submit()
对于一次点击,既可以使用click(),也可以使用一连串的action来实现,如文中:
loadmore=driver.find_element_by_xpath(‘//a[@id="zh-load-more"]‘)
actions = ActionChains(driver)
actions.move_to_element(loadmore)
actions.click(loadmore)
actions.perform()
这5句话其实就相当于一句话,find element然后click,但是action的适用范围更广,譬如在这个例子中,要点击的是一个a标签对象,我不知道为什么直接用click不行,不起作用
print driver.current_url
print driver.page_source
打印网页的两个属性:url和source
转载http://www.phperz.com/article/15/0829/117337.html
selenium+phantomjs解析JS的更多相关文章
- 爬虫:selenium + phantomjs 解决js抓取问题(一)
selenium模块主要用来做测试,模拟键盘.鼠标来操作浏览器. phantomjs 就像一个无界面的浏览器一样. 两个结合能很好的解决js抓取的问题. 测试代码: #coding=utf-8 fro ...
- Python selenium+phantomjs的js动态爬取
Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Chrome等.Phanto ...
- [Python爬虫] 之十五:Selenium +phantomjs根据微信公众号抓取微信文章
借助搜索微信搜索引擎进行抓取 抓取过程 1.首先在搜狗的微信搜索页面测试一下,这样能够让我们的思路更加清晰 在搜索引擎上使用微信公众号英文名进行“搜公众号”操作(因为公众号英文名是公众号唯一的,而中文 ...
- python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用
我们都知道Selenium是一个Web的自动化测试工具,可以在多平台下操作多种浏览器进行各种动作,比如运行浏览器,访问页面,点击按钮,提交表单,浏览器窗口调整,鼠标右键和拖放动作,下拉框和对话框处理等 ...
- Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页及获取JS返回值
前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但是网页的DOM元素内容却可以动态的变化.如果处理这种网页是还用requests库或者 ...
- Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...
- python selenium+phantomjs alert()弹窗报错
问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...
- 基于selenium+phantomJS的动态网站全站爬取
由于需要在公司的内网进行神经网络建模试验(https://www.cnblogs.com/NosenLiu/articles/9463886.html),为了更方便的在内网环境下快速的查阅资料,构建深 ...
- 爬虫之 图片懒加载, selenium , phantomJs, 谷歌无头浏览器
一.图片懒加载 懒加载 : JS 代码 是页面自然滚动 window.scrollTo(0,document.body.scrollHeight) (重点) bro.execute_ ...
随机推荐
- Python学习系列(七)( 数据库编程)
Python学习系列(七)( 数据库编程) Python学习系列(六)(模块) 一,MySQL-Python插件 Python里操作MySQL数据库,需要Python下安装访 ...
- python使用wxPython创建一个简单的文本编辑器。
ubuntu下通过'sudo apt-get install python-wxtools'下载wxPython.load和save函数用于加载和保存文件内容,button通过Bind函数绑定这两个函 ...
- pidstat
统计系统上的某个进程占用的磁盘读写 pidstat -d -p pidNumber 3 -d 表示磁盘设备 -p 指定pid 3 表示每三秒刷新一次结果
- 1108 Finding Average
题意:根据条件判定哪些数是合法的,哪些是不合法的.求其中合法的数的平均值. 思路:字符串处理函数,考虑到最后输出的时候需要控制格式,因此选用scanf()和printf().另外需要了解atof()函 ...
- 严谨的程序案例Api
文档 功能 同步推荐关系 接口方法 syncRelation 参数描述 OriginalUsername 查询的用户用户名 RecommandUsername 推荐人用户名 返回值 status 1成 ...
- python开发mysql:视图、触发器、事务、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- 第八章 JVM内存管理
8.1 物理内存与虚拟内存 地址总线(连接处理器和RAM或处理器和寄存器的)的宽度影响了物理地址的索引范围,决定了处理器一次可以从寄存器或内存中获取多少个bit.同时决定了处理器最大的寻址空间,32位 ...
- Bash脚本编程总结
bash脚本编程之用户交互: read [option]… [name …] -p ‘PROMPT’ -t TIMEOUT bash -n /path/to/some_script 检测脚本中的 ...
- IdentityHashMap
区别与其他的键不能重复的容器,IdentityHashMap允许key值重复,但是——key必须是两个不同的对象,即对于k1和k2,当k1==k2时,IdentityHashMap认为两个key相等, ...
- xUtils怎么post请求上传json数据
InfoSmallCodeBinding smallCode = new InfoSmallCodeBinding(); smallCode.setSmallCode("测试"); ...