【发布时间】: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