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
本文介绍了如何使用PhantomJS和Selenium进行网页自动化操作,包括网页截图、URL获取等基本功能。通过编写简单的JavaScript脚本,可以实现对网页的自动化访问与数据抓取。
2102

被折叠的 条评论
为什么被折叠?



