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

Posted 一起来学python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
});












































以上是关于phantomjs 截取twitter的网页(动态生成的页面)的主要内容,如果未能解决你的问题,请参考以下文章

PhatomJS截取整个网页屏幕为一张图片

调试 PhantomJS 网页.打开失败

动态网页爬取样例(WebCollector+selenium+phantomjs)

python+selenium+PhantomJS爬取网页动态加载内容

如何用python+selenium+phantomjs获得一个网页的动态生成的html代码

Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页