node.js - 使用 superagent 抓取 UTF-8网站乱码
问题描述
我使用 superagent + cheerio 去抓取*东的商品 但是返回的中文乱码 响应头如下 是经过 gzip压缩的 但是按道理 superagent好像会默认解压
*东商品地址https://item.jd.com/5025518.html
我取了商品标题 结果如下
核心代码如下:
var url = ’https://list.jd.com/list.html?cat=670,671,672’ //京东电脑var totalData = [] // 存储总数据superagent.get(url).end(function (err, res) { if (err) {return console.error(err) } var topicUrls = []; // 页面里面的所有url var $ = cheerio.load(res.text) // 拿到页面 $(’#plist .gl-item’).each(function (i, e) {$e = $(e)var href = ’https:’ + $e.find(’.p-img >a’).attr(’href’) // 拿到所有urltopicUrls.push(href) }) var ep = new eventproxy();// //异步调用结束后,执行某些操作 ep.after(’topic_html’, topicUrls.length, function (topics) { //接收res.texttopics = topics.map(function (topicHtml) { var $ = cheerio.load(topicHtml, {decodeEntities: false}); return ({title: $(’.sku-name’).text().trim() });});totalData.push(topics)console.log(totalData); }) topicUrls.forEach(function (e) {superagent.get(e).end(function (err, res) { ep.emit(’topic_html’, res.text);}) })})
问题解答
回答1:这并不是乱码啊,如果是乱码,所有的都乱了,应该是你编码的问题。
回答2:我直接用postman去请求了你那个地址,并没有出现你这种情况,所以我判断是你IDE或者文本编辑器的问题,将格式设为 utf-8 。
相关文章:
1. python - 请问为什么csdn登录后,浏览器控制台reponse返回的是空2. pdo 写入到数据库的内容为中文的时候写入乱码3. 网页爬虫 - python+requests 网页重定向求解4. PHP类封装的插入数据,总是插入不成功,返回false;5. 二维数组怎么重新组合6. python - 斗鱼关注人数爬下来是张加载图片,如何爬取关注人数7. javascript - vue1.0在微信浏览器的兼容性?8. 如何使用mysql查询每个用户一次操作时记录的值9. mysql 时间类型 字段(精确到时分秒) 怎么比较?10. thinkphp中Controller里面使用Request::File(’image’);
![android-studio - 一直无法下载android studio的 安卓模拟器求助](http://www.haobala.com/attached/image/news/202310/095450fe73.jpg)