我正在尝试从这个 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());
}()
答案 0 :(得分:0)
我滚动到错误的元素,使用 ApplyFilter
但是,当您向下滚动时,他之前的结果似乎会被清除,因此需要另一种策略来全部清除。