PhantomJS快速入门教程

官网去下载压缩包 解压出来就行 设置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();
    });
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,314评论 19 139
  • 前言 大家有没有发现之前我们写的爬虫都有一个共性,就是只能爬取单纯的html代码,如果页面是JS渲染的该怎么办呢?...
    追不到的那缕风阅读 8,083评论 1 5
  • 《裕语言》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 10...
    叶染柒丶阅读 28,335评论 5 19
  • 《ilua》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 1...
    叶染柒丶阅读 13,778评论 0 11
  • 所谓的代沟,只不过是我已离开,你却刚来,你所前行的路途我曾经走过,如你所愿,是我在这等待你的到来,还是退回来与你并肩。
    心途中阅读 1,248评论 0 0