Cheerioerror handling程序节点js

我开始使用Node.js一个新的项目,我想知道是否有任何方法来pipe理从cheerio.load()函数返回什么。 我试图使用callback和承诺(然后赶上),但它没有工作。

例:

var $ = cheerio.load(html); //what if it's falied ???? how I can handle it? 

我问这是因为我试图运行脚本仆人时间,但有时它是工作,有时不。

顺便说一句:我使用npm站点的cheerio模块 – > npm install cheerio.

谢谢 :-)

您可以使用try / catch块,因为您不知道cheerio操作是否成功。

 const cheerio = require('cheerio') try { const $ = cheerio.load(html) } catch (e) { console.log(e) // handle error } console.log('continue script')