phantomJS抓取多页会导致执行顺序错误
phantomJS crawling multi pages get misorder execution
我正在使用PhantomJS对从输入流生成的页面进行爬网。但是resultjson和Headers总是{},即使状态是"成功"(第一个url可以是好的,但通常第二个开始是{})。有人能告诉我它出了什么问题吗?
var page = require('webpage').create();
var system = require('system');
var args = system.args;
var fs = require('fs');
var resultjson = {};
var Headers = {};
var urlType, url, path;
var isInUse = false;
function pageInit(page){
page.settings.resourceTimeout = 500000;
page.onResourceTimeout = function (request) {
console.log("fail");
}
page.onConsoleMessage = function (msg, lineNum, sourceID) {
//console.log(msg + "at line " + lineNum);
}
page.onResourceReceived = function (response) {
if (response.url === url && response.stage === "end") {
if (response.status === 301 && response.redirectURL !== null) {
url = response.redirectURL;
}
else {
resultjson.Id = response.id;
resultjson.Url = response.url;
resultjson.Time = response.time;
resultjson.BodySize = response.bodySize;
resultjson.ContentType = response.contentType;
resultjson.RedirectURL = response.redirectURL;
resultjson.Stage = response.stage;
resultjson.Status = response.status;
resultjson.StatusText = response.statusText;
response.headers.forEach(function (header) {
Headers[header.name] = header.value;
});
}
}
};
}
function GetOtherPage(url, path) {
page.open(url, function (status) {
console.log(JSON.stringify(resultjson));
console.log(JSON.stringify(Headers));
resultjson = {};
Headers = {};
if (status !== "success") {
//fs.write(path, page.content, 'w');
console.log("fail");
}
else {
fs.write(path, page.content, 'w');
console.log("success");
}
DoTask();
});
}
function DoTask() {
page.close();
page = require('webpage').create();
pageInit(page);
//page.content = "";
urlType = parseInt(system.stdin.readLine());
if (urlType === -1)
phantom.exit();
url = system.stdin.readLine();
path = system.stdin.readLine();
if (urlType === 3) {
GetOtherPage(url, path);
}
}
DoTask();
结果:
3(input)
https://www.google.com(input)
output.html(input)
{}
{}
success
3(input)
https://www.google.com(input)
output.html(input)
{}
{}
success
更新:当你试图通过一个phantomJS进程获取相同的url时,phantomJS会在你的磁盘缓存中获取它们,所以它不会第二次请求url,所以resultjson和Headers是{}。
属性response.url是标准化的,因此它在键入的属性上添加了一个尾随的'/'
。
由于该比较response.url === url
为假,因此它从不进入if
。
使用https://www.google.com/
(带有尾随的'/'
)进行测试,得到以下输出:
3
https://www.google.com/
output.html
{"Id":1,"Url":"https://www.google.com/","Time":"2016-07-29T03:56:24.294Z","ContentType":"text/html; charset=UTF-8","RedirectU
RL":"https://www.google.com.br/?gfe_rd=cr&ei=BNSaV_KoL6KB8QfJrYPoDA","Stage":"end","Status":302,"StatusText":"Found"}
{"Cache-Control":"private","Content-Type":"text/html; charset=UTF-8","Location":"https://www.google.com.br/?gfe_rd=cr&ei=BNSa
V_KoL6KB8QfJrYPoDA","Content-Length":"263","Date":"Fri, 29 Jul 2016 03:56:52 GMT","Alternate-Protocol":"443:quic","Alt-Svc":"
quic='":443'"; ma=2592000; v='"36,35,34,33,32,31,30,29,28,27,26,25'""}
success
使用phantomjs v2.1.1测试
相关文章:
- javascript函数和代码隐藏函数的执行顺序
- Node.js:多个然后'It’执行顺序不正确
- 不同'单击'不同脚本中的回调:我可以控制执行顺序吗
- 代码混淆的执行顺序
- Javascript执行顺序错误
- Javascript执行顺序和回调
- 奇怪的javascript代码执行顺序
- for 循环/递归中的执行顺序
- JavaScript 中的执行顺序问题
- 来自函数 Javascript 的 NaN 返回值 ||函数执行顺序
- 控制承诺执行顺序
- 我可以信任 JavaScript 中声明中的执行顺序吗?
- 使用webcomponenetsjs的HTMLImports以意外的执行顺序加载导入-firefox
- 确保执行顺序:javascript
- Javascript代码的执行顺序
- 保证HTML表单提交和jQuery onclick的执行顺序
- 在ASP.Net中运行javascript时的执行顺序
- jQuery函数执行顺序
- Javascript中绑定到事件的函数的执行顺序
- 淘汰js可观察扩展的执行顺序是什么