【问题标题】:Web-scraping - How to navigate whenever there is an available link with Puppeteer JSWeb-scraping - 如何在 Puppeteer JS 有可用链接时进行导航
【发布时间】:2021-11-18 17:26:09
【问题描述】:

我想对 url https://data.anbima.com.br/debentures/AGRU12/agenda... 中的主表格正文中的所有数据执行网络 scraping... 但是,由于它实现了分页,因此我无法轻松完成。 ..我想出了以下代码,但它不起作用...我收到错误ReferenceError: list is not defined,尽管我在while循环之前定义了它...

const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(`https://data.anbima.com.br/debentures/AGRU12/agenda`);
  await page.waitForSelector('.normal-text');
  var list = [];
  while (true) {
    let nextButton;
    await page.evaluate(async () => {
      const nodeList = document.querySelectorAll(
        '.anbima-ui-table > tbody > tr'
      );
      let nodeArray = [...nodeList];
      nextButton = document.querySelector('.anbima-ui-pagination__next-button');

      let listA = nodeArray
        .map((tbody) => [...tbody.children].map((td) => [...td.children]))
        .map((tr) =>
          tr.map((span) =>
            span[0].innerHTML
              .replace('<label class="flag__children">', '')
              .replace('</label>', '')
          )
        );
      list.push(listA);
    });

    if (!nextButton) {
      break;
    } else {
      await page.goto(nextButton.href);
    }
  }

  fs.writeFile('eventDates.json', JSON.stringify(list[0], null, 2), (err) => {
    if (err) throw new Error('Something went wrong');

    console.log('well done you got the dates');
  });
  await browser.close();
})();

【问题讨论】:

    标签: javascript while-loop puppeteer goto recurrence


    【解决方案1】:

    List 在回调函数中未定义。您需要在 page.evaluate 中返回数组,然后使用返回的数组将其推送到列表中。

    const list = [];
    while (true) {
        let nextButton;
        const listA = await page.evaluate(async () => {
            const nodeList = document.querySelectorAll(
                '.anbima-ui-table > tbody > tr'
            );
            let nodeArray = [...nodeList];
            nextButton = document.querySelector('.anbima-ui-pagination__next-button');
    
            return nodeArray
                .map((tbody) => [...tbody.children].map((td) => [...td.children]))
                .map((tr) =>
                    tr.map((span) =>
                        span[0].innerHTML
                            .replace('<label class="flag__children">', '')
                            .replace('</label>', '')
                    )
                );
        });
        list.push(...listA);
    

    编辑:更正了我示例中的最后一行。

    【讨论】:

    • 如果有nextButton,你也需要返回,因为没有它:if (!nextButton) 将永远是未定义的
    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多