我爱编程

PhantomJS快速入门教程

2017-06-29  本文已影响0人  海藻web开发

官网去下载压缩包 解压出来就行 设置bin目录环境变量 有个examples文件夹 里边有大量例子
参考文档

example1 hello

//新建js文件 hello.js
console.log("hello phantomJS");
phantom exit();
命令行运行   phantomjs hello.js
输出 hello phantomJS

其中exit是退出phantom 的 否则会一直运行

example2 传参

运行的时候可以直接传递参数 书写格式是这样的
新建arguments.js文件,代码如下

var system = require('system');
if (system.args.length === 1) {
    console.log('Try to pass some args when invoking this script!');
} else {
    system.args.forEach(function (arg, i) {
            console.log(i + ': ' + arg);
    });
}
phantom.exit();
phantomjs arguments.js foo bar

网页访问 截图 牛逼功能的开始

var page = require('webpage').create();
page.open('http://example.com', function () {
    page.render('example.png');
    phantom.exit();
});

open访问网页 open(url,function(status){
if (status !== 'success') {
console.log('FAIL to load the address');
} else{}
})
render方法 会把网页 的快照保存
下边这个例子 传入一个url 别忘记带上http协议 输出访问网页花费了多长时间

var system = require('system'),
    page = require('webpage').create(),
    address,t;
if (system.args.length === 1) {
    console.log('Try to pass some args when invoking this script!');
    phantom.exit();
}
address=system.args[1];
t=Date.now(); //获取当前时间戳
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();
})

evaluate 获取window下的变量 不能拿到闭包里的东西

一个显示网页标题的例子

var page = require('webpage').create();
page.open(url, function (status) {
    var title = page.evaluate(function () {
        return document.title;
    });
    console.log('Page title is ' + title);
});

操作dom的开始

下面的 useragent.js 将读取 id 为qua的元素的 textContent 属性:

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent1111111111';
page.open('http://www.httpuseragent.org', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var ua = page.evaluate(function () {
            return document.getElementById('qua').textContent;
        });
        console.log(ua);
    }
    phantom.exit();
});

一个使用Juqery库的例子

var page = require('webpage').create();
page.open('http://www.sample.com', function() {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
        page.evaluate(function() {
            $("button").click();
        });
        phantom.exit()
    });
});

终极牛逼技能拔取网页请求 和收到的响应的数据

var page = require('webpage').create(),
    system = require('system'),
    address;

if (system.args.length === 1) {
    console.log('Usage: netlog.js <some URL>');
    phantom.exit(1);
} else {
    address = system.args[1];

    page.onResourceRequested = function (req) {
        console.log('requested: ' + JSON.stringify(req, undefined, 4));
    };

    page.onResourceReceived = function (res) {
        console.log('received: ' + JSON.stringify(res, undefined, 4));
    };

    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        }else{
            console.log('over')
        }
        phantom.exit();
    });
}
上一篇下一篇

猜你喜欢

热点阅读