您的位置:首页 > 编程语言 > Python开发

python phantomjs+ selenium2 抓取动态js网页(版本python2.7+)

2016-05-19 11:40 555 查看

1、下载phantomjs linux版本

解压即可

单独使用方法:../bin/phantomjs create_img.js xxx.com(可选)

示例,抓取网页截图:

建立一个pageload.js文件

编写以下js

var page = require('webpage').create();
page.open('http://cuiqingcai.com', function (status) {
console.log("Status: " + status);
if (status === "success") {
page.render('example.png');
}
phantom.exit();
});


运行

phantomjs pageload.js


会在当前目录下看到截图

2、pip安装selenium

pip install selenium

3、selenium+phantomjs 结合写python文件

#!/usr/bin/python
#encoding:utf-8

from selenium import webdriver
#写phantomjs执行文件地址
driver = webdriver.PhantomJS(executable_path='/usr/local/soft_download/python_file/phantomjs/bin/phantomjs')
driver.get("http://mofangdata.cn/")
#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()
注意给python 执行权限 chmod u+x 文件名

运行 ./xx.py
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: