Cheerio错误处理程序节点js

时间:2017-09-15 00:12:58

标签: javascript node.js web-scraping cheerio

我使用Node.js开始一个新项目,我想知道是否有办法管理cheerio.load()函数返回的内容。 我试图使用回调和承诺(然后和捕获),但它确实没有用。

示例:

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

我问这个是因为我试图运行脚本服务时间,但有时它会工作,有时候不工作。

顺便说一句: 我在npm网站上使用cheerio模块 - > npm install cheerio.

谢谢: - )

1 个答案:

答案 0 :(得分:1)

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

const cheerio = require('cheerio')

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

console.log('continue script')
相关问题