node.js - 使用 superagent 抓取 UTF-8網(wǎng)站亂碼
問題描述
我使用 superagent + cheerio 去抓取*東的商品 但是返回的中文亂碼 響應(yīng)頭如下 是經(jīng)過 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 = []; // 頁面里面的所有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();// //異步調(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);}) })})
問題解答
回答1:這并不是亂碼啊,如果是亂碼,所有的都亂了,應(yīng)該是你編碼的問題。
回答2:我直接用postman去請(qǐng)求了你那個(gè)地址,并沒有出現(xiàn)你這種情況,所以我判斷是你IDE或者文本編輯器的問題,將格式設(shè)為 utf-8 。
相關(guān)文章:
1. javascript - vue提示語法錯(cuò)誤,請(qǐng)問錯(cuò)誤在哪?2. 淺談vue生命周期共有幾個(gè)階段?分別是什么?3. index.php錯(cuò)誤,求指點(diǎn)4. css - 關(guān)于偽類背景問題5. javascript - 為什么我的animation-fill-mode 設(shè)置不生效6. html - JavaScript的Dom操作如何改變子元素的文本內(nèi)容7. css - 移動(dòng)端 oppo 手機(jī)之 Border-radius8. python - 抓包只抓到j(luò)son,真實(shí)的地址卻找不到9. java - web端百度網(wǎng)盤的一個(gè)操作為什么要分兩次請(qǐng)求服務(wù)器, 有什么好處嗎10. javascript - vue.js如何遞歸渲染組件.
