node.js - 使用 superagent 抓取 UTF-8網(wǎng)站亂碼
問(wèn)題描述
我使用 superagent + cheerio 去抓取*東的商品 但是返回的中文亂碼 響應(yīng)頭如下 是經(jīng)過(guò) gzip壓縮的 但是按道理 superagent好像會(huì)默認(rèn)解壓

*東商品地址https://item.jd.com/5025518.html
我取了商品標(biāo)題 結(jié)果如下
核心代碼如下:
var url = ’https://list.jd.com/list.html?cat=670,671,672’ //京東電腦var totalData = [] // 存儲(chǔ)總數(shù)據(jù)superagent.get(url).end(function (err, res) { if (err) {return console.error(err) } var topicUrls = []; // 頁(yè)面里面的所有url var $ = cheerio.load(res.text) // 拿到頁(yè)面 $(’#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();// //異步調(diào)用結(jié)束后,執(zhí)行某些操作 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);}) })})
問(wèn)題解答
回答1:這并不是亂碼啊,如果是亂碼,所有的都亂了,應(yīng)該是你編碼的問(wèn)題。
回答2:我直接用postman去請(qǐng)求了你那個(gè)地址,并沒(méi)有出現(xiàn)你這種情況,所以我判斷是你IDE或者文本編輯器的問(wèn)題,將格式設(shè)為 utf-8 。
相關(guān)文章:
1. vim - docker中新的ubuntu12.04鏡像,運(yùn)行vi提示,找不到命名.2. mac連接阿里云docker集群,已經(jīng)卡了2天了,求問(wèn)?3. docker網(wǎng)絡(luò)端口映射,沒(méi)有方便點(diǎn)的操作方法么?4. 為什么我ping不通我的docker容器呢???5. angular.js - angular內(nèi)容過(guò)長(zhǎng)展開(kāi)收起效果6. phpstudy8.1沒(méi)集成mysql-front7. html5和Flash對(duì)抗是什么情況?8. 我何時(shí)應(yīng)該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)9. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!10. 前端 - @media query 使用出現(xiàn)的問(wèn)題?

網(wǎng)公網(wǎng)安備