1. 程式人生 > >json中中文為5位數字的編碼

json中中文為5位數字的編碼

原始碼:

import requests
from html.parser import unescape
from urllib.parse import quote
headers = {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Host': 'shop.m.taobao.com',
    'Origin': 'http://shop.m.taobao.com',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
}
url  = "http://shop.m.taobao.com/shop/shopsearch/search_page_json.do?isb=&sort=default&q=%E5%B0%8F%E5%91%86%E5%91%86%E9%9B%B6%E9%A3%9F%E5%BA%97&_input_charset=utf-8"
response = requests.get(url,headers).text
# data = unescape(response)
data = response
print(type(data))
print(data)

# datas = json.loads(data)
# print(datas)
# if response:
#     data = json.loads(response.decode('utf-8'))
#
# print(data)

例項:

{"result":true,"listItem":[{"area":"","shop":cdn.com/bao/uploaded/bf/d8/TB1NOy2KFXXXXb6XFXXSutbFXXX.jpg_80x80.jpg","keyBiz":"零食 g 小吃 休闲 味 

解決方案:

import requests
from html.parser import unescape
from urllib.parse import quote
headers = {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Host': 'shop.m.taobao.com',
    'Origin': 'http://shop.m.taobao.com',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
}
url  = "http://shop.m.taobao.com/shop/shopsearch/search_page_json.do?isb=&sort=default&q=%E5%B0%8F%E5%91%86%E5%91%86%E9%9B%B6%E9%A3%9F%E5%BA%97&_input_charset=utf-8"
response = requests.get(url,headers).text
data = unescape(response)
print(type(data))
print(data)
例項:

{"result":true,"listItem":[{"area":"","shop":{"name":"小呆呆零食店","logo":"http://gw1.alicdn.com/bao/uploaded/bf/d8/TB1NOy2KFXXXXb6XFXXSutbFXXX.jpg_80x80.jpg","keyBiz":"零食 g 小吃 休閒 味 特...","id":"104541593","isMall":false,"url":"http://shop.m.taobao.com/shop/shop_index.htm?}
 

原因: