【发布时间】:2017-04-21 09:31:22
【问题描述】:
所以这是我用来抓取我的页面的代码(我正在使用 request 和 Cheerio 模块:
for (let j = 1; j < nbRequest; j++)
{
const currentPromise = new Promise((resolve, reject) => {
request(
`https://www.url${j}`,
(error, response, body) => {
if (error || !response) {
console.log("Error: " + error);
}
console.log("Status code: " + response.statusCode + ", Connected to the page");
var $ = cheerio.load(body);
let output = {
ranks: [],
names: [],
numbers: [],
};
$('td.rangCell').each(function( index ) {
if ($(this).text().trim() != "Rang")
{
output.ranks.push($(this).text().trim().slice(0, -1));
nbRanks = nb_ranks+1;
}
});
$('td.nameCell:has(label)').each(function( index ) {
output.names.push($(this).find('label.nameValue > a').text().trim());
});
$('td.numberCell').each(function( index ) {
if ($(this).text().trim() != "Nombre")
{
output.numbers.push($(this).text().trim());
}
});
console.log("HERE 1");
return resolve(output);
}
);
});
promises.push(currentPromise);
}
之后,我使用节点模块解析并将结果保存在 csv 文件中。 在这一点上,我已经能够爬取大约 100 页,但是当涉及到更大的数字(1000+)时,我收到了 500 个响应,这意味着我认为我被踢了。 所以我认为最好的解决方案是延迟请求,但我没有找到解决方案。 你们有任何想法以及代码的外观吗?
【问题讨论】: