1. 下载链接 http://code.google.com/p/phantomjs/downloads/list
2. 无需安装 windows 将解压后的exe路径加到环境变量
3. 网站分析 phantomjs提供了很多的examples,其中netsniff.js就是用于监控网络请求并生成HAR文件的。例如:
F:\phantomjs-1.9.2-windows\examples>phantomjs netsniff.js http://www.qq.com > test
生成har格式如下
{
"log": {
"version": "1.2",
"creator": {
"name": "PhantomJS",
"version": "1.9.2"
},
"pages": [
{
"startedDateTime": "2014-05-22T07:33:37.107Z",
"id": "http://baidu.com",
"title": "",
"pageTimings": {
"onLoad": 16
}
}
],
"entries": [
{
"startedDateTime": "2014-05-22T07:33:37.073Z",
"time": 48,
"request": {
"method": "GET",
"url": "http://baidu.com/",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.2 Safari/534.34"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
],
"queryString": [],
"headersSize": -1,
"bodySize": -1
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [
{
"name": "Last-Modified",
"value": "Tue, 12 Jan 2010 13:48:00 GMT"
},
{
"name": "Accept-Ranges",
"value": "bytes"
},
{
"name": "Content-Length",
"value": "81"
},
{
"name": "Content-Type",
"value": "text/html"
},
{
"name": "Date",
"value": "Thu, 22 May 2014 06:34:24 GMT"
},
{
"name": "Server",
"value": "Apache"
},
{
"name": "ETag",
"value": "\"51-4b4c7d90\""
},
{
"name": "Expires",
"value": "Fri, 23 May 2014 06:34:24 GMT"
},
{
"name": "Cache-Control",
"value": "max-age=86400"
},
{
"name": "Age",
"value": "3552"
},
{
"name": "X-Cache",
"value": "HIT from SK-SQUIDWEB-18"
},
{
"name": "X-Cache-Lookup",
"value": "HIT from SK-SQUIDWEB-18:8080"
},
{
"name": "Via",
"value": "1.1 SK-SQUIDWEB-18:8080 (squid/2.7.STABLE9)"
}
],
"redirectURL": "",
"headersSize": -1,
"bodySize": 81,
"content": {
"size": 81,
"mimeType": "text/html"
}
},
"cache": {},
"timings": {
"blocked": 0,
"dns": -1,
"connect": -1,
"send": 0,
"wait": 47,
"receive": 1,
"ssl": -1
},
"pageref": "http://baidu.com"
}
]
}
}
4. 获取网页截图 保存下面代码为test.js 使用命令phantomjs test.js生成'example.png' 是网页的截图。
var page = require('webpage').create();
page.open('http://www.baidu.com', function() {
page.render('example.png');
phantom.exit();
});
5、HAR Viewer(http://www.softwareishard.com/har/viewer/)是基于Javascript的一个很强大的HAR分析工具,它可以将HAR文件以瀑布流和饼图等形式进行展示。源代码在这里:http://code.google.com/p/harviewer/。使用简单,在线粘贴har文件内容,或者将har文件拖进窗口即可。