【问题标题】:Infinite scroll with puppeteer带木偶的无限卷轴
【发布时间】:2021-05-21 03:05:27
【问题描述】:

我正在尝试从这个page 中抓取数据。该页面在 DOM 中的一个元素内具有无限滚动。我有以下puppeteer 脚本可以一直滚动到底部以显示所有数据,以便我可以抓取它。第一个page.evaluate 调用按预期工作,滚动到底部并显示更多内容。但是,后续调用不起作用。我尝试使用await page.waitFor(2000); 等待我可以进一步滚动,但它似乎不起作用。请帮助别人!

// this script parses the data from http://covid.rcmedicrew.org/ using their /scripts/getSearch.php API
// install nodejs then npm install axios
const puppeteer = require('puppeteer');

const siteURL = "https://www.powerbi.com/view?r=eyJrIjoiOTcyM2JkNTQtYzA5ZS00MWI4LWIxN2UtZjY1NjFhYmFjZDBjIiwidCI6ImQ1ZmE3M2I0LTE1MzgtNGRjZi1hZGIwLTA3NGEzNzg4MmRkNiJ9";


async function callPowerBiApi(apiUrl) {
  const browser = await puppeteer.launch({headless: false, args: ['--auto-open-devtools-for-tabs']});
  // const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  page.setDefaultNavigationTimeout(0);
  await page.goto(apiUrl, {waitUntil: 'networkidle0', timeout: 0});
  await page.waitForSelector('.innerContainer');

  await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div>div div:last-child").scrollIntoView());
  //await page.waitFor(2000);
  await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div>div div:last-child").scrollIntoView());
  await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div>div div:last-child").scrollIntoView());
  await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div>div div:last-child").scrollIntoView());
  await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div>div div:last-child").scrollIntoView());
  await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div>div div:last-child").scrollIntoView());
}()

【问题讨论】:

  • 滚动到错误的元素,修复它await page.evaluate(() => document.querySelectorAll(".innerContainer")[1].querySelector(".bodyCells>div div:last-of-type div:last-of-type div:last-of-type").scrollIntoView());
  • 很高兴听到 - 随时添加self answer

标签: javascript node.js puppeteer infinite-scroll


【解决方案1】:

我滚动到错误的元素,通过使用修复它:

await page.evaluate(() => document.querySelectorAll(".innerContainer")[1]
    .querySelector(".bodyCells>div div:last-of-type div:last-of-type div:last-of-type")
    .scrollIntoView()
);

但是,当您向下滚动时,以前的结果似乎会被清除,因此需要另一种策略来将其全部清除。

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 2023-03-11
    • 2019-02-05
    • 2013-05-05
    • 2014-08-18
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多