一、page对象抓取页面并存储为图像(以etao为例,以下若不另外说明,均为此)注意:phantom.exit()放在page.open()里为宜why?不知var page = require('webpage').create();page.open('http://www.etao.com',function(){page.render('etao.png');phantom.exit();});抓取结果如下:

不足之处:1、分辨率低。2、抓取页面内容宽度默认为990px,可以通过page.viewportSize = {width:2200,height:2000}来修改虚拟页面的大小。需要改进:1、图像存储路径问题能否解决?2、能否写成调用模式?
二、测试页面加载速度代码示例A:var page = require('webpage').create(), t, address;if (phantom.args.length === 0) { console.log('Usage:loadspeed.js <some URL>'); phantom.exit();}
t = Date.now();address =phantom.args[0];page.open(address, function(status) { if (status != 'success'){ console.log('FAIL to load the address'); } else { t = Date.now() - t; console.log('Loading time ' + t + 'msec'); } phantom.exit();});
代码示例B:var page = require('webpage').create(), system =require('system'), t, address;
if (system.args.length === 1) { console.log('Usage:loadspeed.js <some URL>'); phantom.exit();}
t = Date.now();address = system.args[1];page.open(address, function (status) { if (status !=='success') { console.log('FAIL to load the address'); } else { t = Date.now() - t; console.log('Loading time ' + t + 'msec'); } phantom.exit();});不足之处:1、每次测量结果不同,平均结果约为2300+msec2、phantom.exit()之后不需要return,否则出现syntax error:parse error
三、执行js代码evaluate()可以返回一个简单对象,但是不能返回函数或者闭包。下面的代码实现的是提取页面的title,并显示。注意:1、从webpage上console.log的信息默认状态下,不会被显示,即便是evaluate()中的代码。为了显示,需要调用page的onConsoleMessage事件。2、page.evaluate()是个sandbox,只能执行()里网页中的代码和进行一些DOM操作,且()里的代码不能获取()之外的对象和变量,比如,page的方法与函数等。疑问:如何向evaluate()中传递参数呢???完整代码如下:var page = require('webpage').create();page.onConsoleMessage = function(msg){console.log('Page title is' + msg);};var url=phantom.args[0];page.open(url,function(status){page.evaluate(function(){console.log(document.title);});phantom.exit();});不足: 1、汉字显示的是乱码,,,不知如何解决。
四、https://github.com/ariya/phantomjs/blob/master/examples/imagebin.js中的源码//Upload an image to imagebin.org
var page=require('webpage').create(),system=require('system'),fname;
if(system.args.length!== 2){console.log('Usage:imagebin.js filename');phantom.exit(1);} else{fname=system.args[1];page.open("http://imagebin.org/index.php?page=add",function (){page.uploadFile('input[name=image]',fname);page.evaluate(function() {document.querySelector('input[name=nickname]').value= 'phantom';document.querySelector('input[name=disclaimer_agree]').click()document.querySelector('form').submit();});window.setTimeout(function() {phantom.exit();},3000);});}为何自己写的,就有错呢?varpage = require('webpage').create();page.open('http://www.weibo.com/',function (status) {if (status === 'fail') { console.log('loadfailed'); } else{console.log("12");page.evaluate(function(){ document.querySelector('div#pl_login_form.W_login_forminput[name=username]').value = 'zhuxi';document.querySelector('div#pl_login_form.W_login_forminput[name=password]').value = '82';document.querySelector('div#pl_login_form.W_login_formdiv.info_list a.W_btn_g span').click();});} page.close(); phantom.exit();});显示错误为:null is not a object(evaluate 'document.queryS......hui'')
分析:服务器登陆都有严格的流程,一般都会经过HTTPS协议,POST密码,而且是加密过的,还会经过302跳转等,因此这种直接通过GET的登陆肯定行不通,不过可以看看phantomjs是否有类似于curl或者java中的httpclient的api供使用模拟登陆。