我想要爬取下面图片框选区域内的所有音乐会:
https://istack.dev59.com/7QIMM.webp
问题在于这个列表只呈现前10个选项,直到你在特定的div中向下滚动到底部,然后它才会动态呈现更多内容,直到没有更多结果为止。 我试着按照以下链接的答案操作,但无法滚动以展示所有的“音乐会”:
这是我的基本代码:
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)
}
}
document.querySelectorAll('.odIJnf').length >> 20 document.querySelector("#Q5Vznb").scrollIntoView(false); >> undefined document.querySelectorAll('.odIJnf').length
所以,在滚动命令后,音乐会数量增加了10。 - Benny