最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - scrollIntoView() looping element on puppeteer - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

With 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);
}
发布评论

评论列表(0)

  1. 暂无评论