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 \数\据\返\回\成\功
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.
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)
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
(ie signature utf-8 file).
Using utf-8-sig
to read a file will treat BOM as file info instead of a string.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.