PhantomJS是一个服务器端的 JavaScript API 的 WebKit,支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, SVG
PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
PhantomJS 介绍
1)一个基于webkit内核的无头浏览器,没有UI界面,即它就是一个浏览器,只是其内的点击、翻页等人为相关操作需要程序设计实现
2)提供JavaScript API接口,即通过编写js程序可以直接与webkit内核交互,在此之上可以结合Java语言等,通过java调用js等相关操作,从而解决了以前c/c++才能比较好的基于webkit开发优质采集器的限制
3)提供Windows、Linux、Mac等不同os的安装使用包,也就是说可以在不同平台上二次开发采集项目或是自动项目测试等工作
PhantomJS 官网: http://phantomjs.org
使用场景:
-
无需浏览器的 Web 测试
-
页面访问自动化
-
屏幕捕获
-
网络监控
屏幕捕获示例代码:
1 2 3 4 5 |
|
PhantomJS 生态环境:
-
CasperJS enables easy navigation scripting and common high-level testing.
-
Poltergeist allows running Capybara tests headlessly.
-
Guard::Jasmine automatically tests Jasmine specs on Rails when files are modified.
-
GhostDriver complements Selenium tests with a PhantomJS WebDriver implementation.
-
PhantomRobot runs Robot Framework acceptance tests in the background via PhantomJS.
-
Mocha-PhantomJS run Mocha tests using PhantomJS.
- 其他一些相关项目
PhantomJS 常用API
1)常用内置几大对象
1 2 3 |
|
2) 常用API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
3)注意事项
- 区分phantomjs的对象和打开的web page的对象,如document、window等,两者都有,在调用page.evaluate和不调用的时候,注意区分二者的范围,容易在调试时出现很多的问题,且不好发现。
- page.injectJs和page.includeJs的区别,前者侧重本地的js文件,与libraryPath挂购,后者侧重网络js文件,尤其在引入jQuery等第三方库时,会经常遇到。
- 编码问题,两个重要参数,--output-encoding,--script-encoding,前者为输出编码,后者为所使用js、参数配置文件的编码,为方便起鉴,建议均采用utf-8编码,并注所应用到的目标文件的编码,以免引起很不可思议的异常,又无从查起。
1. 下载 PhantomJS
官网下载: http://phantomjs.org/download.html
Windows: phantomjs-2.1.1-windows.zip
Mac OS X:phantomjs-2.1.1-macosx.zip (推荐)
Linux 64-bit:phantomjs-2.1.1-linux-x86_64.tar.bz2(推荐)
Linux 32-bit: phantomjs-2.1.1-linux-i686.tar.bz2
FreeBSD: sudo pkg install phantomjs
PhantomJS 源码: https://github.com/ariya/phantomjs/
2. 安装 PhantomJS
PhantomJS不需要安装,解压后,配置环境变量后,便可直接使用
3. 配置 PhantomJS
1) macOS PATH 变量
vim /etc/profile
添加如下内容:
PHANTOMJS_HOME=/opt/phantomjs-2.1.1
export PATH=$PHANTOMJS_HOME/bin:$PATH
执行命令,使其生效:
source /etc/profile
验证可用:
$ phantomjs -v
2.1.1
2) Windows PATH 变量
在“系统变量”选项区域中查看PATH变量,如果不存在,则新建变量 PATH, 否则选中该变量,
单击“编辑”按钮,在“变量值”文本框的起始位置添加 D:\phantomjs\bin
开始-运行-输入cmd,输入 phantomjs --version
如果可以看到版本号,则安装成功。
4. 简单示例
PhantomJS 自带示例目录:
cd /opt/phantomjs-2.1.1/examples
hello.js 示例:
1 2 3 |
|
执行命令,运行结果:
$ phantomjs hello.js
Hello, world!
命令行执行:
$ phantomjs
phantomjs> console.log('hello mimvp.com'); phantom.exit();
hello mimvp.com
爬取网页:
vim spider_web.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
模拟登陆示例:
vim login.js
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
执行命令:
plantomjs login.js >> index.html
说明:plantomjs非常方便的应用于模拟登陆,如微博、电商类,或是小米、火车票抢票等项目中
5. 代理示例
PhantomJS 帮助文档里说明,只支持 http 和 socks5 两种协议类型,不支持 https 和 socks4
如果要访问 http 和 https 网站,推荐 http 访问 http网站,socks5 访问 http 和 https 网站
phantomjs --help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
如上,代理参数分别为:
--proxy=<val> Sets the proxy server, e.g. '--proxy=http://proxy.company.com:8080'
--proxy-auth=<val> Provides authentication information for the proxy, e.g. ''-proxy-auth=username:password'
--proxy-type=<val> Specifies the proxy type, 'http' (default), 'none' (disable completely), or 'socks5'
PhantomJS 代理示例,由两部分组成,分别是爬取网页代码和使用代理命令(采用米扑代理,测试成功)
1) 爬取网页代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
2)使用代理命令
http 协议的执行命令:
phantomjs --proxy-type=http --proxy=138.68.161.157:8080 mimvp-phantomjs.js
socks5 协议的执行命令:
phantomjs --proxy-type=socks5 --proxy=82.209.150.212:45454 mimvp-phantomjs.js
测试代理来自米扑代理(推荐):