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. docker-compose 為何找不到配置文件?2. boot2docker無法啟動3. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?4. java中返回一個對象,和輸出對像的值,意義在哪兒5. android - E/dalvikvm: Could not find class java.nio.file.Path,6. python - linux怎么在每天的凌晨2點執行一次這個log.py文件7. android - 哪位大神知道java后臺的api接口的對象傳到前端后輸入日期報錯,是什么情況?求大神指點8. mysql數據庫每次查詢是一條線程嗎?9. python是怎么實現過濾 #注釋代碼的?10. 請問一下各位老鳥 我一直在學習獨孤九賤 現在是在tp5 今天發現 這個系列視頻沒有實戰
