使用puppeteer循环遍历表行

时间:2022-12-20 19:24:20

I want to get link from each row in a table. That's how I get 3rd (for example):

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

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

I want to iterate through table rows, I use such code:

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

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);}

When i execute code, i get such error:

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

(node:16928) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: i is not definedat <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.jsprocess with a non-zero exit code.

How should it be done correctly?

如何正确地完成?

1 个解决方案

#1


2  

Good dayYou can get the content of the page and then with the help of cheerio go through elements and parse them.

你可以得到页面的内容,然后在cheerio的帮助下浏览元素并解析它们。

#1


2  

Good dayYou can get the content of the page and then with the help of cheerio go through elements and parse them.

你可以得到页面的内容,然后在cheerio的帮助下浏览元素并解析它们。