如果您可以成功地从第一个 <p> 获取 URL,那么您已经知道要执行此操作的所有内容,所以我想您对 request 的工作方式有问题,尤其是基于回调的工作流程。
我的建议是删除 request,因为它已被弃用。您可以使用基于 Promise 的 got 之类的东西,因此您可以使用它附带的较新的 async/await 功能(这通常意味着更轻松的工作流程)(不过,您至少需要使用 nodejs 8!) .
您的循环将如下所示:
for (const i = 0; i < urls.length; i++) {
const source = await got(urls[i]);
// Do your cheerio determination
console.log(new_p.html());
}
请注意,您的函数签名需要调整。在您的情况下,您根本没有指定函数,因此使用了模块的函数签名,这意味着您不能使用await。所以为此编写一个函数:
async function pullAllUrls() {
const mainSource = await got(main_url);
...
}
如果你不想使用async/await,你可以减少一些承诺,但我认为这相当麻烦。然后回到 Promise 并使用像 async 这样的工作流库来帮助您管理 URL 获取。
使用 async/await 的真实示例:
在一个真实的例子中,我会创建一个函数来获取我想要获取的页面的源代码,就像这样(不要忘记将 got 添加到你的 script/package.json):
async function getSourceFromUrl(thatUrl) {
const response = await got(thatUrl);
return response.body;
}
然后你有一个工作流逻辑来获取其他页面中的所有这些链接。我是这样实现的:
async function grabLinksFromUrl(thatUrl) {
const mainSource = await getSourceFromUrl(thatUrl);
const $ = cheerio.load(mainSource);
const hrefs = [];
$('ul.menu__main-list').each((i, content) => {
$('li a', content).each((idx, inner) => {
const wantedUrl = $(inner).attr('href');
hrefs.push(wantedUrl);
});
}).get();
return hrefs;
}
我决定要获取<nav> 元素中的链接,这些链接通常包含在<ul> 和<li> 的元素中。所以我们只拿那些。
然后您需要一个工作流程来处理这些链接。这就是for 循环所在的位置。我决定要每页的标题。
async function mainFlow() {
const urls = await grabLinksFromUrl('https://netzpolitik.org/');
for (const url of urls) {
const source = await getSourceFromUrl(url);
const $ = cheerio.load(source);
// Netpolitik has two <title> in their <head>
const title = $('head > title').first().text();
console.log(`${title} (${url}) has source of ${source.length} size`);
// TODO: More work in here
}
}
最后,您需要调用该工作流函数:
return mainFlow();
您在屏幕上看到的结果应如下所示:
Dossiers & Recherchen (https://netzpolitik.org/dossiers-recherchen/) has source of 413853 size
Der Netzpolitik-Podcast (https://netzpolitik.org/podcast/) has source of 333354 size
14 Tage (https://netzpolitik.org/14-tage/) has source of 402312 size
Official Netzpolitik Shop (https://netzpolitik.merchcowboy.com/) has source of 47825 size
Über uns (https://netzpolitik.org/ueber-uns/#transparenz) has source of 308068 size
Über uns (https://netzpolitik.org/ueber-uns) has source of 308068 size
netzpolitik.org-Newsletter (https://netzpolitik.org/newsletter) has source of 291133 size
netzwerk (https://netzpolitik.org/netzwerk/?via=nav) has source of 299694 size
Spenden für netzpolitik.org (https://netzpolitik.org/spenden/?via=nav) has source of 296190 size