phantomjs学习笔记

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 Viewerhttp://www.softwareishard.com/har/viewer/)是基于Javascript的一个很强大的HAR分析工具,它可以将HAR文件以瀑布流和饼图等形式进行展示。源代码在这里:http://code.google.com/p/harviewer/。使用简单,在线粘贴har文件内容,或者将har文件拖进窗口即可。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值