Phantom JS
PhantomJs 用户处理网页中JS加载较多的情况。当然我问也可以用分析每一个js链接的跳转,但一旦这样的文件过多,这件事意见繁琐的事情。而PhantomJS就是一个不错的选择
1.安装PhantomJs
安装网址:http://phantomjs.org/download.html 选择相应的版本和系统
建议为PhantomJS设置环境变量。在下载的安装包中,有一个example文件夹,里面有很多官方的例子可供学习和参考
安装完成后,在命令行输入:phantomgjs -v. 如果正常显示版本号,则证明安装匹配成功。
2.快速入门
(1)输出一个hello world
新建javascript文件hello.js
console.log('Hello,world!'); //在控制太输出Hello world!
phantom.exit(); //退出程序
在命令行中输入:pyantomjs hello.js 回车
(2.)页面的加载,以及保存截图
1 var page=require('webpage').creater(); //使用webpage 创建一个page对象 2 page.open('http://www.cnblogs.com/qiyeboy/',function(status){ 3 console.log("status:"+status); 4 if(status==="success"){ 5 page.render('qiye.png') //通过render方法将当前页面保存为qiye.png 6 } 7 phantom.exit(); 8 })
#pageload.js
在命令行:phantomjs pageload.js
(3.)计算网页的加载速度
var page= require('webpage').create(), system= require('system'), t,address; if(system.args.length ===1){ console.log('Usage:loadspeed.js<some URL>'); phantomjs.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; consloe.log('Loading')+system.args[1]); consloe.log('Loading time'+t+'msec'); } phantom.exit() })
在命令行输入:phantomjs loadspeed.js http://www.cnblogs.com/qiyeboy/
输出结果:
Loading http://www.cnblogs.com/qiyeboy/
Loading time 793 msec
(4)代码评估
var url ='http://www.cnblogs.com/qiyeboy/';
var page= require('webpage').create();
page.open(url,function(status){
var title= page.evaluate(function(){
return document.title;
});
console.log('page title is '+title);
phantom.exit();
});
在命令行输入:phantomjs evaluate.js
任何来自网页并且包括来自evaluate()内部代码的控制台信息,默认不会显示。要覆盖此行为,使用onConsoleMessage回调方法。
var url ='http://www.cnblogs.com/qiyeboy/';
var page= require('webpage').create();
page.onConsoleMessage= function(status){
console.log(document.title);
};
page.open(url,function(status){
page.evaluate(function(){
consloe.log(document.title);
});
phantom.exit();
})
在命令行中输入:pantomjs evaluate.js
输出结果:
page title is 七夜的故事 -博客园
(5)屏幕捕获功能
var page= require('webpage').create(); page.viewportSize={width:1204,height:768}; //视图区的大小 page.clipRect={top:0,left:0,width:512,height:256};//在这个视图区中裁剪矩形的大小,前两个是基准点,后两个是宽高 page.open('http://www.cnblogs.com/qiyeboy/',function(status){ console.log('status:'+status); if (status==='success'){ page.render('qiye.png'); } phantom.exit(); })
(6)网络监控
var url ='http://www.cnblog.com/qiyeboy/'; var page= require('webpage').create(); page.onRexourceRequested=function(request){ console.log('Request'+JSON.stringify(request,undefined,4)); }; page.ONResourceReceived=function(reqponse){ console.log('Receive'+JSON.stringify(response,undefined,4)); }; page.open(url);
命令行运行:phantomjs netmonitor.js
(7)页面自动化 phantomJS中标准javaScript的DOM操作和CSS选择器是生效的
var page=require('webpage').create(); console.log('The default user agent is'+page.settings.userAgent); page.settings.userAgent='Mozilla/5.0 (windows NT 6.1;WOW64;rv:49.0) Gecko/20100101 Firefox/49.0' page.open('http://movie.mtime.com/108737/',function(status){ if (status!==='success'){ console.log('Unable to access network'); }else{ var ua =page.evaluate(function(){ return document.getElementById('ratingRegion').textContent; }); console.log(ua); } phantom.exit(); });
代码解释:
首先创建page对象,接着将默认的User-Agent进行修改,打开指定网页,当加载完成之后,执行DOM操作,获取id为ratingRedion元素下的内容,并打印出来。
默认的UserAgent的内容,会发现里面包含了phantomJS关键字,一些网站就是通过这个关键字来识别是否正在使用Phantomjs爬取数据
更多资料转载博客:https://www.cnblogs.com/bangejingting/p/6907628.html
浙公网安备 33010602011771号