【发布时间】:2021-08-01 01:08:19
【问题描述】:
所以我试图刮掉下图中框出区域的所有音乐会:
https://i.stack.imgur.com/7QIMM.jpg
问题是列表仅显示前 10 个选项,直到您在该特定 div 中向下滚动到底部,然后它会动态显示更多选项,直到没有更多结果。我尝试按照以下链接的答案进行操作,但无法向下滚动以呈现所有“音乐会”:
How to scroll inside a div with Puppeteer?
这是我的基本代码:
const browser = await puppeteerExtra.launch({ args: [
'--no-sandbox'
]});
async function functionName() {
const page = await browser.newPage();
await preparePageForTests(page);
page.once('load', () => console.log('Page loaded!'));
await page.goto(`https://www.google.com/search?q=concerts+near+poughkeepsie&client=safari&rls=en&uact=5&ibp=htl;events&rciv=evn&sa=X&fpstate=tldetail`);
const resultList = await page.waitForSelector(".odIJnf");
const scrollableSection = await page.waitForSelector("#Q5Vznb"); //I think this is the div that contains all the concert items.
const results = await page.$$(".odIJnf"); //this needs to be iterable to be used in the for loop
//this is where I'd like to scroll down the div all the way to the bottom
for (let i = 0; i < results.length; i++) {
const result = await (await results[i].getProperty('innerText')).jsonValue();
console.log(result)
}
}
【问题讨论】:
标签: javascript node.js web-scraping puppeteer infinite-scroll