从PhantomJS网站上的Ajax请求中获取状态代码
Get Status Code from an Ajax request on the site in PhantomJS
我们正在使用PhantomJS访问我们公司的内部网站并截图。
该脚本基本上是PhantomJS的官方示例之一,添加了waitTime变量(https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js)
var page = require('webpage').create(),
system = require('system'),
address, output, size;
//waitTime is how long to to wait for kibana and the data to load in millis.
//Increase this if your queries appear incomplete in your PDFs. 10s seems to work for me.
var waitTime = 10 * 1000;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');
console.log(' "800px*600px" window, clipped to 800x600');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
// Uncomment the following line to login. Replace user:pass with your username and password.
//page.customHeaders={'Authorization': 'Basic '+btoa('user:pass')};
page.viewportSize = { width: 1280, height: 720 };
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
size = system.args[3].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
: { format: system.args[3], orientation: 'portrait', margin: '1cm' };
} else if (system.args.length > 3 && system.args[3].substr(-2) === "px") {
size = system.args[3].split('*');
if (size.length === 2) {
pageWidth = parseInt(size[0], 10);
pageHeight = parseInt(size[1], 10);
page.viewportSize = { width: pageWidth, height: pageHeight };
page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight };
} else {
console.log("size:", system.args[3]);
pageWidth = parseInt(system.args[3], 10);
pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any
console.log ("pageHeight:",pageHeight);
page.viewportSize = { width: pageWidth, height: pageHeight };
}
}
if (system.args.length > 4) {
page.zoomFactor = system.args[4];
}
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, waitTime);
}
});
}
我们正在访问的网站正在运行JavaScript,即对数据库进行请求。但是,根据给定的查询,此请求可能需要很长时间(最多10分钟)。
因此,我们想做的不是提供静态的10分钟等待时间,而是从访问的网站上运行的JavaScript中提取状态代码,如果它达到200,脚本应该进行"截屏"。
我们的问题是,我们不知道如何从网站本身获取信息。(我们如何克服使用的PhantomJS脚本和访问的网站之间的"桥梁"?)
这就是page.onResourceReceived
的作用。
page.onResourceReceived = function(response) {
if (response.stage === "end" && response.url.indexOf("/longrunningrequest") !== -1) {
setTimeout(function(){
if ("200" === (""+response.status)) {
page.render("screenshot_200.png");
} else {
page.render("screenshot_other.png");
}
phantom.exit();
}, 1000);
}
};
page.open(address);
您可以从URL中确定应该触发屏幕截图的特定响应。我使用了一个简单的字符串匹配,但您也可以使用正则表达式之类的。
当然,如果您通过node.js中的一个可用网桥使用PhantomJS,情况会有所不同。
相关文章:
- 获取选择框的状态
- 获取ASP.NET Ajax Timer状态
- 使用PHP或Javascript获取服务器状态
- JavaScript和Html代码来运行所有类型的视频并获取状态
- 使用php的Ajax,获取状态200,但readyState为0
- 从PhantomJS网站上的Ajax请求中获取状态代码
- 可折叠面板获取状态
- 如何从URL中获取状态变量值
- 在angular js promise中未获取状态错误代码
- 使用Angular.js ui路由器获取状态错误
- 使用JS SDK获取状态
- 从url获取状态
- 嵌套异步获取状态的React组件会导致Flux分发链
- 获取状态点击
- 如何从URL获取状态码
- 从$返回的jqXHR中获取状态码失败.post调用
- 从youtube API获取状态而不使用swfobject.embedSWF
- Javascript 从服务中获取状态
- 获取状态为已选中和未选中的所有复选框的列表
- 使用YouTube Iframe API内部的jquery插件来获取状态