我有一个phantomJS脚本,其中包含以下内容:
- page.open(url,function (status) {
- if (status === "fail") { /* handle failure */ }
- });
解决方法
你可以这样做:
- var page = require('webpage').create(),system = require('system'),resources = [];
- page.open('http://google.com',function (status) {
- console.log('Loaded with http status:',resources[0].status);
- phantom.exit();
- });
- page.onResourceReceived = function(response) {
- // check if the resource is done downloading
- if (response.stage !== "end") return;
- // apply resource filter if needed:
- if (response.headers.filter(function(header) {
- if (header.name == 'Content-Type' && header.value.indexOf('text/html') == 0) {
- return true;
- }
- return false;
- }).length > 0)
- resources.push(response);
- };
因此,如果您需要检查第一个浏览器请求的状态(在本例中为google的html页面),您应该将其视为资源[0] .status中返回的第一个请求.在onResourceReceived处理程序中,您可以为尝试从中获取http代码的资源添加更多过滤器.