木偶-滚动无限页面在无头模式下不起作用

时间:2018-12-11 11:29:18

标签: javascript puppeteer headless

我无法正确运行相同的代码。

我收到此错误:导航超时超出:超过30000ms

我该如何解决?

const puppeteer = require('puppeteer')
const cheerio = require('cheerio');

(async () => {
  const browser = await puppeteer.launch({headless: true})
  const page = await browser.newPage()
  await page.setRequestInterception(true)
  page.on('request', request => {
    if (request.resourceType() === 'image') {
      request.abort()
    } else {
      request.continue()
    }
  })
  await page.goto('http://amzn.eu/ebKNEqg')
  await scroll(page)

  let content = await page.content()
  let $ = cheerio.load(content)
  const whishlist = []
  $('h3.a-size-base a.a-link-normal').each((i, elem) => {
    whishlist[i] = $(elem).attr('href')
  })
  console.log(whishlist)
  await browser.close()
})()

async function scroll (page) {
  let i = 0
  while (await page.$('.wl-see-more') !== null) {
    i++
    // Scroll to bottom to load more
    await page.evaluate(_ => {
      window.scrollBy(0, window.innerHeight)
    })
    console.log('Loop #' + i)
  }
}

如果您有其他适用的提示,我也尝试通过阻止图像的加载来加速人偶,我很感兴趣!

0 个答案:

没有答案