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. javascript - 怎么實現移動端頁面滑動切換,從1可以滑到2 但是不能從2滑回1 這樣的效果呢?2. javascript - JS設置Video視頻對象的currentTime時出現了問題,IE,Edge,火狐,都可以設置,反而chrom卻...3. 求大神幫我看看是哪里寫錯了 感謝細心解答4. php自學從哪里開始?5. 網頁爬蟲 - python爬蟲翻頁問題,請問各位大神我這段代碼怎樣翻頁,還有價格要登陸后才能看到,應該怎么解決6. javascript - 如果所有請求都放到actions 里面,那拿到的數據應該 放在哪里,state 還是vue實例里面的data?7. 數據庫 - MySQL 單表500W+數據,查詢超時,如何優化呢?8. mysql多表查詢9. phpstady在win10上運行10. python - from ..xxxx import xxxx到底是什么意思呢?
