Python Requests response decode
Solution 1
Use requests.Response.json and you will get the chinese caracters.
import requests
import json
url = "http://www.biyou888.com/api/getdataapi.phpac=3005&sess_token=&urlid=-1"
res = requests.get(url)
if res.status_code == 200:
res_payload_dict = res.json()
print(res_payload_dict)
Solution 2
How to use response.encoding using Python requests?
For illustrate, let’s ping API of Github.
# import requests module
import requests
# Making a get request
response = requests.get('https://api.github.com')
# print response
print(response)
# print encoding of response
print(response.encoding)
output:
<Response [200]>
utf-8
So, in your example, try res.json()
instead of res.text
?
As in:
# encoding of your response
print(res.encoding)
res.encoding='utf-8-sig'
print(res.json())
sig
in utf-8-sig
is the abbreviation of signature
(i.e. signature utf-8 file).
Using utf-8-sig
to read a file will treat BOM as file info instead of a string.
Solution 3
import requests
import json
url = "http://www.biyou888.com/api/getdataapi.php?ac=3005&sess_token=&urlid=-1"
res = requests.get(url)
res_dict = {}
if res.status_code == 200:
print type(res.text)
print res.text
res_dict = json.loads(res.text) # get a dict from parameter string
print "Dict:"
print type(res_dict)
print res_dict
print res_dict['info']
Use json
module to parse that input. And the prefix u
just means it is a unicode string. When you use them, the u
prefix won't affect, just like what I show at the last several lines.
hcancan
Updated on June 04, 2022Comments
-
hcancan almost 2 years
I send a request using python requests and then print the response, what confuse me is that the Chinese characters in response is something like \u6570\u636e\u8fd4\u56de\u6210\u529f
Here is the code:# -*- coding:utf-8 -*- import requests url = "http://www.biyou888.com/api/getdataapi.php?ac=3005&sess_token=&urlid=-1" res = requests.get(url) if res.status_code == 200: print res.text
Below is the response
What should i do to tranfer the response? I have try to use encode and decode but it dose work.
-
filaton over 6 yearsWhat do you mean by "transfer the response"? What does not work?
-
hcancan over 6 yearsI mean convert the "/u" format charracters into Chinese characters
-
-
Mederic Fourmy over 6 yearsWhat do you mean by it doesn't work, you get an error? The json you have is not the right one? Just edited the 3rd line that was broken.
-
hcancan over 6 yearsI tried your way , But only the last line print Chinese character
-
hcancan over 6 yearsThere is no "?" between host and parameters in your code, I fix it but the result is something like this {u'status': 1, u'info': u'\u6570\u636e\u8fd4\u56de\u6210\u529f', ...}
-
Mederic Fourmy over 6 yearsI am using Python 3, that must be the reason of our difference, I will try with python 2
-
hcancan over 6 yearsThx, I have found the reason why the response text is not Chinese characters by deault, it's because the response return "/u" format Letters, Instead of characters encoded using utf-8
-
lincr over 6 yearsThe 'u' prefix won't bother when you really use the response text. And if you just want to print the Chinese characters out, like my
print
demo shows, use something likeprint res_dict['data'][0]['title']
. -
Mederic Fourmy over 6 yearsTesting the code with python 2 and printing
res_payload_dict['info']
, I got 数据返回成功. Encoding is still puzzling to me to say the least. Anyway, glad you found a solution.