Cheerio的text()对于某些字符返回-(U + FFFD)

时间:2018-10-20 11:34:55

标签: node.js json web-scraping character-encoding cheerio

我正在创建一个网站抓取API,有时会有一些特殊字符,通常在名称中,例如。 “JesúsM.Vargas”。

我的API使用request库下载网站内容,并将页面的正文传递给cheerio,例如:const body = cheerio.load(page);

然后,我尝试在页面上查找文本,我知道它始终是第一个<b>var copyright = body('b').eq(0).text().trim();

接下来,我的代码使用{}之类的代码将所有内容(如标题,版权等)添加到对象(data["copyright"] = copyright;

最后一件事是将对象作为JSON对象返回给用户:

app.get("/api/", (req, res) => {
  res.setHeader('Content-Type', 'application/json');
  // query parameters
  const date = req.query.date;
  .
  . // get the webpage, find the data, add everything to an object
  .
  // get the data from `data` object and return it as JSON
  var output = JSON.stringify(data);
  res.send(output);
}

当API返回上面示例中的文本时,JSON文件中将显示“ Jes。M.Vargas”。有没有办法解决这些字符?为了使用我的原始JavaScript来对对象进行字符串化处理,我的Web框架为Express

0 个答案:

没有答案
相关问题