Python 请求得到 Json 响应,其中某些字段编码不正确
Python request gets Json response with some fields encoded incorrectly
在我正在处理的一个项目中,当我请求从服务器获取 EPG 数据时,Json 响应中的标题和描述是乱码。最初我虽然这可能是一个编码问题,但尝试了 decoding/encoding 各种格式的文本但没有成功。
无论如何,这里是获取带有一些调试输出的请求的代码:
def get_short_epg(profile, stream_id, limit=1):
epg_url = build_url("{0}/player_api.php".format(profile['server']),
{'username': profile['username'], 'password': profile['password'],
'action': 'get_short_epg', 'stream_id': stream_id, 'limit': limit})
response = requests.get(epg_url)
if response.status_code == 200:
if profile['debug']:
print(response.encoding)
print(response.apparent_encoding)
print(response.request.headers)
print(response.json())
print(response.content)
data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
output_file= os.path.join(data_dir, 'short_epg.json')
f = open(output_file, 'w')
f.write(response.text)
f.close()
else:
print("Failed with status code {0}".format(response.status_code))
产生以下输出:
None
ascii
{'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'}
{'epg_listings': [{'id': '1037932301', 'epg_id': '34', 'title': 'QnJpdGFpbidzIEdvdCBUYWxlbnQ=', 'lang': 'en', 'start': '2020-10-03 20:00:00', 'end': '2020-10-03 22:00:00', 'description': 'QW50IGFuZCBEZWMgaG9zdCB0aGUgZmlmdGggc2VtaS1maW5hbCBvZiB0aGlzIHllYXIncyB0YWxlbnQgY29udGVzdCwgd2VsY29taW5nIGJhY2sgdGhlIGFjdHMgdGhhdCBpbXByZXNzZWQgdGhlIGp1ZGdlcyBkdXJpbmcgdGhlIGF1ZGl0aW9ucyBlcGlzb2RlcyBpbiB0aGUgc3ByaW5nLiBBbWFuZGEgSG9sZGVuLCBBbGVzaGEgRGl4b24sIERhdmlkIFdhbGxpYW1zIGFuZCBBc2hsZXkgQmFuam8gYXJlIG9uIHRoZSBqdWRnaW5nIGRlc2su', 'channel_id': 'ITV London', 'start_timestamp': '1601751600', 'stop_timestamp': '1601758800'}]}
b'{"epg_listings":[{"id":"1037932301","epg_id":"34","title":"QnJpdGFpbidzIEdvdCBUYWxlbnQ=","lang":"en","start":"2020-10-03 20:00:00","end":"2020-10-03 22:00:00","description":"QW50IGFuZCBEZWMgaG9zdCB0aGUgZmlmdGggc2VtaS1maW5hbCBvZiB0aGlzIHllYXIncyB0YWxlbnQgY29udGVzdCwgd2VsY29taW5nIGJhY2sgdGhlIGFjdHMgdGhhdCBpbXByZXNzZWQgdGhlIGp1ZGdlcyBkdXJpbmcgdGhlIGF1ZGl0aW9ucyBlcGlzb2RlcyBpbiB0aGUgc3ByaW5nLiBBbWFuZGEgSG9sZGVuLCBBbGVzaGEgRGl4b24sIERhdmlkIFdhbGxpYW1zIGFuZCBBc2hsZXkgQmFuam8gYXJlIG9uIHRoZSBqdWRnaW5nIGRlc2su","channel_id":"ITV London","start_timestamp":"1601751600","stop_timestamp":"1601758800"}]}'
将 json 输出保存到一个文件中,因为怀疑 json 中提供的标题和描述文本可能是这种情况被编码 diffrently.Tried encoding/decoding使用以下内容加载的标题值:
list = json.load(open('data/short_epg.json'))
title = list['epg_listings'][0]['title']
print(title.encode())
for enc in encodings.aliases.aliases:
for enc2 in encodings.aliases.aliases:
try:
test = title.encode(enc2).decode(enc)
if test not in title:
print('{0} to {1} = {2}'.format(enc2, enc, test))
except:
pass
但是导致不同输出的唯一结果是 chinese/arabic 的结果。还尝试了不同的服务器,以防这是孤立的问题,但每次都得到相同的乱码数据。
仅供参考,“QnJpdGFpbidzIEdvdCBUYWxlbnQ=”的标题值应该是“英国达人”,任何人都知道这是哪种错误编码或如何获得正确的文本值。
它是 base64 编码的。 QnJpdGFpbidzIEdvdCBUYWxlbnQ=
变为 Britain's Got Talent
您可以使用 python base64 lib 对其进行解码。
import base64
print(base64.b64decode('QnJpdGFpbidzIEdvdCBUYWxlbnQ=').decode('utf-8'))
在我正在处理的一个项目中,当我请求从服务器获取 EPG 数据时,Json 响应中的标题和描述是乱码。最初我虽然这可能是一个编码问题,但尝试了 decoding/encoding 各种格式的文本但没有成功。
无论如何,这里是获取带有一些调试输出的请求的代码:
def get_short_epg(profile, stream_id, limit=1):
epg_url = build_url("{0}/player_api.php".format(profile['server']),
{'username': profile['username'], 'password': profile['password'],
'action': 'get_short_epg', 'stream_id': stream_id, 'limit': limit})
response = requests.get(epg_url)
if response.status_code == 200:
if profile['debug']:
print(response.encoding)
print(response.apparent_encoding)
print(response.request.headers)
print(response.json())
print(response.content)
data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
output_file= os.path.join(data_dir, 'short_epg.json')
f = open(output_file, 'w')
f.write(response.text)
f.close()
else:
print("Failed with status code {0}".format(response.status_code))
产生以下输出:
None ascii {'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'} {'epg_listings': [{'id': '1037932301', 'epg_id': '34', 'title': 'QnJpdGFpbidzIEdvdCBUYWxlbnQ=', 'lang': 'en', 'start': '2020-10-03 20:00:00', 'end': '2020-10-03 22:00:00', 'description': 'QW50IGFuZCBEZWMgaG9zdCB0aGUgZmlmdGggc2VtaS1maW5hbCBvZiB0aGlzIHllYXIncyB0YWxlbnQgY29udGVzdCwgd2VsY29taW5nIGJhY2sgdGhlIGFjdHMgdGhhdCBpbXByZXNzZWQgdGhlIGp1ZGdlcyBkdXJpbmcgdGhlIGF1ZGl0aW9ucyBlcGlzb2RlcyBpbiB0aGUgc3ByaW5nLiBBbWFuZGEgSG9sZGVuLCBBbGVzaGEgRGl4b24sIERhdmlkIFdhbGxpYW1zIGFuZCBBc2hsZXkgQmFuam8gYXJlIG9uIHRoZSBqdWRnaW5nIGRlc2su', 'channel_id': 'ITV London', 'start_timestamp': '1601751600', 'stop_timestamp': '1601758800'}]} b'{"epg_listings":[{"id":"1037932301","epg_id":"34","title":"QnJpdGFpbidzIEdvdCBUYWxlbnQ=","lang":"en","start":"2020-10-03 20:00:00","end":"2020-10-03 22:00:00","description":"QW50IGFuZCBEZWMgaG9zdCB0aGUgZmlmdGggc2VtaS1maW5hbCBvZiB0aGlzIHllYXIncyB0YWxlbnQgY29udGVzdCwgd2VsY29taW5nIGJhY2sgdGhlIGFjdHMgdGhhdCBpbXByZXNzZWQgdGhlIGp1ZGdlcyBkdXJpbmcgdGhlIGF1ZGl0aW9ucyBlcGlzb2RlcyBpbiB0aGUgc3ByaW5nLiBBbWFuZGEgSG9sZGVuLCBBbGVzaGEgRGl4b24sIERhdmlkIFdhbGxpYW1zIGFuZCBBc2hsZXkgQmFuam8gYXJlIG9uIHRoZSBqdWRnaW5nIGRlc2su","channel_id":"ITV London","start_timestamp":"1601751600","stop_timestamp":"1601758800"}]}'
将 json 输出保存到一个文件中,因为怀疑 json 中提供的标题和描述文本可能是这种情况被编码 diffrently.Tried encoding/decoding使用以下内容加载的标题值:
list = json.load(open('data/short_epg.json'))
title = list['epg_listings'][0]['title']
print(title.encode())
for enc in encodings.aliases.aliases:
for enc2 in encodings.aliases.aliases:
try:
test = title.encode(enc2).decode(enc)
if test not in title:
print('{0} to {1} = {2}'.format(enc2, enc, test))
except:
pass
但是导致不同输出的唯一结果是 chinese/arabic 的结果。还尝试了不同的服务器,以防这是孤立的问题,但每次都得到相同的乱码数据。
仅供参考,“QnJpdGFpbidzIEdvdCBUYWxlbnQ=”的标题值应该是“英国达人”,任何人都知道这是哪种错误编码或如何获得正确的文本值。
它是 base64 编码的。 QnJpdGFpbidzIEdvdCBUYWxlbnQ=
变为 Britain's Got Talent
您可以使用 python base64 lib 对其进行解码。
import base64
print(base64.b64decode('QnJpdGFpbidzIEdvdCBUYWxlbnQ=').decode('utf-8'))