与Nightmare.js串行迭代链接

时间:2018-10-24 16:09:29

标签: javascript web-scraping browser-automation nightmare

以下代码使用google结果页面中的链接名称创建一个数组。我想遍历每个链接,并检查结果中的标题(在HTML内)是否与网站上的标题相同。

const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });

nightmare
.goto('https://www.google.com')
.type('.gsfi', 'wikipedia')
.click('form[action*="/search"] [type=submit]')
.evaluate(() =>
Array.from(document.querySelectorAll('.LC20lb')).map(
  element => element.innerText
))
.then(innerTexts => {
console.log(innerTexts);
})
.catch(error => {
console.error('Search failed:', error);
});

是否可以通过Promise.each做到这一点?

0 个答案:

没有答案