爬虫cheerio乱码问题

没有检索到摘要

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在乱码问题是

res.write(爬到的内容),在页面上显示是正常的

但是直接在node端打印console.log(爬到的内容)是乱码

调整乱码问题的关键属性是cheerio.load中的{decodeEntities:false}属性

代码一

  var cheerio = require('cheerio')
  , superagent = require('superagent')
  , express = require('express');

var url = 'http://acm.hdu.edu.cn/statistic.php?pid=1000';
var app = express();

app.get('/', function (req, res, next) {

  superagent.get(url)
.end(function (err, pres) {
  var html = pres.text;
  var $ = cheerio.load(html, {decodeEntities: false});
  var ans = $('.r_search_item').eq(0).html();
  res.write(ans);
});
  });

app.listen(3000, function () {
  console.log('app is listening at port 3000');
});
复制代码

代码二

 var cheerio = require('cheerio')
  , superagent = require('superagent')
  , express = require('express');

var url = 'http://acm.hdu.edu.cn/statistic.php?pid=1000';
var app = express();

app.get('/', function (req, res, next) {

  superagent.get(url)
.end(function (err, pres) {
  var html = pres.text;
  var $ = cheerio.load(html);
  var ans = $('.r_search_item').eq(0).html();
  res.write(ans);
});
  });

app.listen(3000, function () {
  console.log('app is listening at port 3000');
});
复制代码

总结

当为'代码一'时,添加{decodeEntities: false}属性,node端正常,页面乱码 当为'代码一'时,没有{decodeEntities: false}属性,node端乱码,页面正常

解决方案

www.tuicool.com/articles/6R…

转载于:https://juejin.im/post/5c0640f7f265da615876eb1e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值