关于nodejs爬虫:node爬虫遇到的各种问题cheeriopuppeteer

6次阅读

共计 2186 个字符,预计需要花费 6 分钟才能阅读完成。

工夫:2021 年 4 月 1 号,文中各版本号以该工夫为背景

问题一、网页采纳 gb2312 编码,爬取后中文全副乱码

环境:node@8.12.0,cheerio@0.22.0
网站应用的 gb2312 编码,开始用 http 间接拜访网页,cheerio 加载后 console 进去中文全副乱码:

const http = require('http')
const cheerio = require('cheerio')
const baseUrl = '******'

http.get(baseUrl, res => {
  let html = ''res.on('data', data => {html += data})
  res.on('end', () => {downloadHandler(html)
  })
}).on('error', () => {console.log('出错了')
})
function downloadHandler(html) {const $ = cheerio.load(html) // 默认解析
  console.log($.html());
}

cheerio 解析:

不解析:

function downloadHandler(html) {const $ = cheerio.load(html,{ decodeEntities: false}) // 不解析
  console.log($.html());
}

起因:Node 不反对 gb2312

解决:应用 superagent 取代 http,同时应用 superagent-charset 解决编码问题

const request = require('superagent')
require('superagent-charset')(request)
const cheerio = require('cheerio')
const baseUrl = '******'

request.get(baseUrl)
  .buffer(true)
  .charset('gbk')
  .end((err, html) => {downloadHandler(html)
  })

function downloadHandler(html) {
  const htmlText = html.text
  const $ = cheerio.load(htmlText,{ decodeEntities: false})
}

问题二、一个循环外部,每次循环都会有一个异步申请;循环内部须要等外部所有申请返回后果后再执行

解决:

const idList = [1,2,3,4]
getData(idList).then(data => {// get data})

function getData(idList) {let asyncPool = []
    idList.forEach(id => {asyncPool.push((() => {return new Promise((resolve,reject) => {return request.get(`http://detail/${id}`)
                .buffer(true)
                .charset('gbk')
                .then(html => {return Promise.reslove(html)
                })
            })
        })())
    })
    return Promise.all(asyncPool).then(data => {return data})
}

问题三、运行 puppeteer 报错:unexpected token {

环境:node@8.12.0,npm@6

起因:node 版本过低

解决:降级 node 版本至最新稳定版 14.16.0,重新安装 puppeteer

问题四、运行 puppeteer 报错:could not find expected browser

环境:node@14.16.0,npm@6
降级好 node,重新安装 puppeteer。我亲眼看见他下载了版本号如同是 865 什么的 chromium,而后运行代码 puppeteer 又报错说在本地找不到 865 版本的浏览器(明明方才下载了,且版本号统一)。
最终在官网 issues 里找到了可能的答案:
https://github.com/puppeteer/puppeteer/issues/6586

起因:npm 版本问题。貌似是 npm6 下载 puppeteer 时,理论下载的浏览器版本和 puppeteer 须要的并不统一

解决:降级 npm 至 7,重新安装 puppeteer,失常运行

问题五、降级 node 后,cheerio 无奈失常运行,报错:content.forEach() is not a function

环境:node@14.16.0,npm@7,cheerio@v1.0.0-rc5
降级了 node 和 npm 至最新稳定版后,把之前的包也全副重新安装了一遍
cheerio 忽然始终报错无奈运行,之前是失常的。
还是在官网 issues 里找到了答案:
https://github.com/cheeriojs/cheerio/issues/1591

起因:cheerio 版本问题

Cheerio.load expects there to be array. Older versions was there condition for checking, if it is not array. Seems like it is lost due to optimizations.
older version wraps it into array

解决:重新安装 0.22.0 版本即可

正文完
 0