如何处理gzip以及为何存在一些响应格式问题

时间:2019-05-10 05:02:25

标签: axios jsonp

我最近正在使用axios进行网络抓取,响应是jsonp,这不是主要问题,问题在于某些中文字体。如何解决这个问题? 看起来像这样 https://user-images.githubusercontent.com/22767695/57355572-e9a32500-71a0-11e9-913c-1b42535e41e2.png


const axios = require("axios");
(async () => {
  const productId = "100002928171";
  axios.interceptors.response.use(response => {
    response.headers = {
      age: 0,
      "content-encoding": "gzip",
      "content-type": "application/json;charset=GBK",
      date: "Tue, 07 May 2019 15:17:52 GMT",
      server: "JDWS/2.0",
      status: 200,
      via: "BJ-Y-NX-112(MISS), http/1.1 JN-UNI-1-JCS-32 ( [cMsSf ])"
    };
    return response;
  });
  await axios({
    method: "GET",
    url: `https://sclub.jd.com/comment/productPageComments.action`,
    params: {
      productId,
      score: 0,
      sortType: 5,
      page: 0,
      pageSize: 10,
      isShadowSku: 0,
      fold: 1
    },
    headers: {
      DNT: 1,
      Referer: `https://item.jd.com/${productId}.html`,
      "sec-ch-ua": "Google Chrome 74",
      "Sec-Fetch-Dest": "script",
      "Sec-Fetch-Mode": "no-cors",
      "Sec-Fetch-Site": "same-site",
      "Sec-Fetch-User": "?F",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
    }
  }).then(res => console.log(res.data));
})();

我也用邮递员测试了这个网址,我得到了我想要的东西 https://user-images.githubusercontent.com/22767695/57355759-574f5100-71a1-11e9-97a4-b390237e674b.png

0 个答案:

没有答案
相关问题