I want to scrape a list of posts while the page scroll is infinite loading. I want scrollIntoView()
for each element in the loop. my code is temporarily like this and the result when the page is loaded, it bounces out but no error.
for (let i = 0; i < 20; i++) {
const selector = 'div[role="feed"]:nth-child(2) div.sjgh65i0'
await page.evaluate((selector, i) => {
setTimeout(() => {
const element = document.querySelectorAll(selector)[i]
if(element) {
element.scrollIntoView();
}
}, 2000);
}, selector, i)
}
For example, I want to take 20 posts and then the scraper will take the posts one by one.
I want to scrape a list of posts while the page scroll is infinite loading. I want scrollIntoView()
for each element in the loop. my code is temporarily like this and the result when the page is loaded, it bounces out but no error.
for (let i = 0; i < 20; i++) {
const selector = 'div[role="feed"]:nth-child(2) div.sjgh65i0'
await page.evaluate((selector, i) => {
setTimeout(() => {
const element = document.querySelectorAll(selector)[i]
if(element) {
element.scrollIntoView();
}
}, 2000);
}, selector, i)
}
For example, I want to take 20 posts and then the scraper will take the posts one by one.
Share Improve this question edited Aug 22, 2021 at 18:38 ggorlen 58k8 gold badges114 silver badges157 bronze badges asked Aug 22, 2021 at 13:42 fanonanofanonano 731 silver badge4 bronze badges1 Answer
Reset to default 4With your current flow, all timeouts are set almost at once and then all fire after the same 2 sec.
Try something like this:
const selector = 'div[role="feed"]:nth-child(2) div.sjgh65i0';
for (let i = 0; i < 20; i++) {
await page.waitForTimeout(2000);
await page.evaluate((selector, i) => {
const element = document.querySelectorAll(selector)[i];
if(element) {
element.scrollIntoView();
}
}, selector, i);
}