PHANTOMJS Interception of Twitter pages (dynamically generated pages)

Source: Internet
Author: User

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, which is loaded.
//
Once all resources is loaded, we wait a small amount of time
(resourcewait) In the case these resources load the other resources.
//
The page is rendered after a maximum amount of time (maxrendertime)
The 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 Interception of Twitter pages (dynamically generated pages)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.