【发布时间】:2015-07-17 20:37:41
【问题描述】:
在遇到麻烦(第一个计时器 nodejs 和 casperjs/phantomjs)之后,它开始工作了。我使用 curl(php) 完成了这项工作。
这是我试图完成的:
- 登录
- 获取所有单位
- 解析他们的详细信息
- (我的问题)2 个单元的详细信息由 ajax 调用提供
casper.start(url, function() {
this.evaluate(function() {
document.querySelector("input[name='username']").value = "username";
document.querySelector("input[name='password']").value = "passwrd";
document.querySelector("#login").click();
});
console.log("Logged in..");
});
var processPage = function() {
console.log("Get all units..");
var units = this.evaluate(getUnits);
allUnits.push(units);
if (!this.evaluate(isLastPage)) {
this.thenClick('.paging li:last-child a').then(function() {
currentPage++;
console.log("Stepping to page.. " + currentPage);
this.waitFor(function() {
return currentPage === this.evaluate(getSelectedPage);
}, processPage, terminate);
});
} else{
require('utils').dump(allUnits);
casper.then(function() {
this.capture('test.png');
});
console.log("Total unit count: " + allUnits.length);
}
};
casper.waitForSelector('.units', processPage, terminate);
casper.run();
在下面的函数中,我解析了行,我也想添加 ajax 获取的 2 个详细信息,但我不知道该怎么做。 (异步)
function getUnits() {
var rows = document.querySelectorAll('.units');
var units = [];
for (var i = 0, row; row = rows[i]; i++) {
var aID = row.querySelector('a').getAttribute('href').split('/');
unit['id'] = aID[2];
//add other details for the unit
**//Do a async call to the 2 external links with the ID and add the details to the unit**
units.push(unit);
}
return units;
};
需要注意的重要一点是,最后我想在单元上运行另一个函数,但在运行之前必须已经获取所有函数......
编辑
登录后页面显示一个表格,我得到的表格是这样的
- 身份证
- 所有者
- 街道
- 被关注 (对带有帖子的链接进行的自动 ajax 调用)
- PLACEDABIDON(对与 发布)
我尝试正常使用 casper 获取最后 2 个字段,但有时它得到了值,有时它没有(请求有时太慢)
我想知道的是,如何在不等待每一行(单位)获得值的情况下获取这些字段。 (所以每个单元都应该为他们自己获取值并将它们填充到他们的对象中。所以可能需要一个回调?
或者我可以自己做请求,我只需要 ID 和 cookie 来发帖(链接将 ID 和 Cookie 作为参数)并获取详细信息并填写,但我不知道该怎么做或者如果第一个解决方案效果更好,或者即使这是可能的......
最重要的是,在所有单元都有其详细信息之后,它应该继续应用程序的逻辑......
【问题讨论】:
标签: javascript asynchronous phantomjs screen-scraping casperjs