【问题标题】:Loop through table rows with puppeteer使用 puppeteer 循环遍历表行
【发布时间】:2017-10-27 20:31:38
【问题描述】:

我想从表中的每一行获取链接。这就是我获得第三名的方式(例如):

await page.evaluate(() => document.querySelector(#content-tabs-0 > div > table > tbody > tr:nth-child(3) > td.mpt-1-td-desc > a").href)

我想遍历表格行,我使用这样的代码:

for(let i=1;i<=10;i++){
let link = await page.evaluate(() => document.querySelector("#content-tabs-0 
> div > table > tbody > tr:nth-child("+i+") > td.mpt-1-td-desc > a").href);
console.log(link);
}

当我执行代码时,我得到这样的错误:

(node:16928) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: i is not defined
at <anonymous>:1:87
(node:16928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.

应该如何正确做?

【问题讨论】:

    标签: node.js web-scraping google-chrome-headless puppeteer


    【解决方案1】:

    美好的一天 您可以获取页面的内容,然后在 Cheerio 的帮助下遍历元素并解析它们。

    【讨论】:

    • 工作简单快速,谢谢 :) 但我认为 puppeteer 中应该有一些内置功能让事情变得更简单
    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 2011-07-19
    • 1970-01-01
    • 2020-08-27
    • 2019-05-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多