phantomjs 截取twitter的网页(动态生成的页面)

// This example shows how to render pages that perform AJAX calls
// upon page load.
//
// Instead of waiting a fixed amount of time before doing the render,
// we are keeping track of every resource that is loaded.
//
// Once all resources are loaded, we wait a small amount of time
// (resourceWait) in case these resources load other resources.
//
// The page is rendered after a maximum amount of time (maxRenderTime)
// or if no new resources are loaded.

//page.onResourceRequested =  function (requestData, networkRequest) {}  //page的所要加载的资源在发起请求时,都可以回调该函数
//page.onResourceReceived =  function (response) {}  //page的所要加载的资源在加载过程中,每加载一个相关资源,都会在此先做出响应,它相当于


var fs = require("fs");
var resourceWait = 300,
maxRenderWait = 10000,
url='https://twitter.com/CXA10';

var page = require('webpage').create(),
count = 0,
forcedRenderTimeout,
renderTimeout;

page.viewportSize = { width: 1280, height : 1024 };

function doRender() {
page.render('twitter.png');
fs.write('1.html', page.content, 'w');
phantom.exit();
}

page.onResourceRequested = function (req) {
count += 1;
console.log('> ' + req.id + ' - ' + req.url);
clearTimeout(renderTimeout);
};

page.onResourceReceived = function (res) {
if (!res.stage || res.stage === 'end') {
count -= 1;
console.log(res.id + ' ' + res.status + ' - ' + res.url);
if (count === 0) {
renderTimeout = setTimeout(doRender, resourceWait);
}
}
};

page.open(url, function (status) {
if (status !== "success") {
console.log('Unable to load url');
// phantom.exit();
} else {
forcedRenderTimeout = setTimeout(function () {
console.log(count);
doRender();
}, maxRenderWait);
}
});

转载于:https://www.cnblogs.com/c-x-a/p/6349598.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值