请求时的编码问题 Use body.encode(‘utf-8‘) if you want to send it encoded in UTF-8
生活随笔
收集整理的這篇文章主要介紹了
请求时的编码问题 Use body.encode(‘utf-8‘) if you want to send it encoded in UTF-8
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天發(fā)現一個網站,新榜,是各大網站,視頻好的排行網站,請求時動態(tài)網頁的xhr。網絡里選擇xhr
刷新就可以看到數據。
代碼如下:
import requestsheaders = {'authority': 'www.newrank.cn','sec-ch-ua': '"Microsoft Edge";v="95", "Chromium";v="95", ";Not A Brand";v="99"','accept': 'application/json, text/javascript, */*; q=0.01','content-type': 'application/json;charset=UTF-8','x-requested-with': 'XMLHttpRequest','sec-ch-ua-mobile': '?0','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4613.0 Safari/537.36 Edg/95.0.997.1','sec-ch-ua-platform': '"Windows"','origin': 'https://www.newrank.cn','sec-fetch-site': 'same-origin','sec-fetch-mode': 'cors','sec-fetch-dest': 'empty','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ko;q=0.5,ja;q=0.4', }params = (('nonce', '6fddb2680'),('xyz', '5bb76228dc539601e197d148ca2d19a9'), )data = '{"numeric":"\u5A31\u4E50","rankDate":"2021-09-07","start":1,"size":50,"rankType":"0","type":"0"}'response = requests.post('https://www.newrank.cn/nr/bili/rank/complexMainRank', headers=headers, params=params, data=data.encode('utf-8'))#NB. Original query string below. It seems impossible to parse and #reproduce query strings 100% accurately so the one below is given #in case the reproduced version is not "correct". # response = requests.post('https://www.newrank.cn/nr/bili/rank/complexMainRank?nonce=6fddb2680&xyz=5bb76228dc539601e197d148ca2d19a9', headers=headers, data=data)print(response.status_code) print(response.json())?這里面有一個坑就是 data。如果不加encode ,就會返回這樣的結果:
?
?這還是編碼的問題。這里body其實就是data
?
加上這個就好了。?
?結果返回:
?
總結
以上是生活随笔為你收集整理的请求时的编码问题 Use body.encode(‘utf-8‘) if you want to send it encoded in UTF-8的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫,爬取猫眼电影2(xpa
- 下一篇: 使用geopandas 制作中国疫情地图