鍍金池/ 問答/HTML/ koa2 UnhandledPromiseRejectionWarning

koa2 UnhandledPromiseRejectionWarning

  1. 運(yùn)行下面代碼后報(bào)錯(cuò),不知道怎么修改,望大神指出錯(cuò)誤點(diǎn)。
  2. 報(bào)錯(cuò)
(node:2920) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejec
tion id: 1): Error: Navigation Timeout Exceeded: 30000ms exceeded
(node:2920) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
 Node.js process with a non-zero exit code.
  1. 代碼如下
const puppeteer = require('puppeteer')

const url = `https://movie.douban.com/tag/#/?sort=R&range=6,10&tags=%E7%94%B5%E5%BD%B1,%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A`

const sleep = time => new Promise(resolve => {
  setTimeout(resolve, time)
})

;(async () => {
  console.log('Start visit the target page')

  const browser = await puppeteer.launch({
    args: ['--no-sandbox'],
    dumpio: false
  })

  const page = await browser.newPage()
  await page.goto(url, {
    waitUntil: 'networkidle2'
  })

  await sleep(3000)

  await page.waitForSelector('.more')

  for (let i = 0; i < 2; i++) {
    await sleep(3000)
    await page.click('.more')
  }

  const result = await page.evaluate(() => {
    var $ = window.$
    var items = $('.list-wp a')
    var links = []

    if (items.length >= 1) {
      items.each((index, item) => {
        let it = $(item)
        let doubanId = it.find('div').data('id')
        let title = it.find('.title').text()
        let rate = Number(it.find('.rate').text())
        let poster = it.find('img').attr('src').replace('s_ratio', 'l_ratio')

        links.push({
          doubanId,
          title,
          rate,
          poster
        })
      })
    }

    return links
  })

  browser.close()

  process.send({result})
  process.exit(0)
})()
回答
編輯回答
櫻花霓

剛好我也在練手這個(gè)項(xiàng)目,你加梯子,就可以爬到了,默認(rèn)欲歌瀏覽器給墻了,現(xiàn)在雙層墻。

2017年12月13日 20:13
編輯回答
離人歸

首先你這個(gè)錯(cuò)誤信息,是你沒有處理promise的錯(cuò)誤情況,你要在你async函數(shù)中添加try....catch進(jìn)行異常捕獲,方便調(diào)試錯(cuò)誤信息

第二你用 puppeteer 獲取信息的代碼是正確的,你把

process.send({result})
  process.exit(0)

注釋掉就正確了

你看是不是你的主進(jìn)程報(bào)的錯(cuò)

2017年3月7日 11:23