Python3 尝试解码 (base64) 然后解压缩 (gzip) 时出错
Python3 error trying to decode (base64) then decompress (gzip)
我正在尝试解码 json 文本,然后在解码后使用 gzip 将其解压缩
但无论我如何尝试去做,我都会遇到这个错误
" File "D:\VS Code\VSC Projects\api.py", line 25, in <module>
decoded = BytesIO(decompress(b64decode(raw)))
File "C:\Users\paz-pc\AppData\Local\Programs\Python\Python39\lib\base64.py", line 80, in b64decode
s = _bytes_from_decode_data(s)
File "C:\Users\paz-pc\AppData\Local\Programs\Python\Python39\lib\base64.py", line 45, in _bytes_from_decode_data
raise TypeError("argument should be a bytes-like object or ASCII "
TypeError: argument should be a bytes-like object or ASCII string, not 'dict'
这是我的代码:
import requests
from mojang import MojangAPI
from pprint import pprint
from base64 import b64decode
from gzip import decompress
from io import BytesIO
name = str(input("Enter your name of choice >>>"))
api_key = "cant show you the key"
uuid = MojangAPI.get_uuid(name)
request_url = (str("https://api.hypixel.net/player?key=" + api_key + "&uuid=" + uuid))
hydata = requests.get(request_url).json()
raw = hydata["player"]["stats"]["Pit"]["profile"]["inv_contents"]
file1 = open("ProfessionalNames.json", 'w')
file1.write(str(raw))
file1.close()
file2 = open("ProfessionalNames.json", 'r')
decoded = BytesIO(decompress(b64decode(raw)))
pprint((decoded))
这就是 ProfessionalNames.json 中的内容,也就是我要解码 (base64) 然后解压缩 (gzip) 的内容
{'type': 0, 'data': [31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -107, 83, -37, 110, -45, 64, 16, 29, -25, -46, 38, 41, 106, -112, 0, 85, 2, -124, 70, 2, -98, 40, -120, 10, 104, -59, 99, 27, 90, 69, -94, -108, 10, 8, -81, 104, 108, 79, -20, 85, -100, -35, 104, 119, 77, 91, -15, 65, -111, -8, -116, 124, 8, -97, 2, -116, 93, 92, -110, 42, -118, 96, 95, -68, 62, 51, 103, -9, -52, -52, -39, 14, 64, 27, 2, -43, 1, -128, 71, 53, -88, -87, 56, -88, 5, -48, -20, -103, 92, -5, -96, 3, 117, 79, 73, 0, 27, 3, 29, 90, -90, 17, -123, 25, 7, 117, 104, -9, 85, -52, 71, 25, 37, 78, 72, 63, 59, -80, 30, 43, 55, -55, -24, -94, 13, -115, 99, 99, -71, 37, 104, 3, 110, -52, -90, 123, -89, 108, 71, -88, 60, -113, 5, -71, 53, -101, -66, 126, -78, -121, -5, -34, 83, 52, -62, 55, 52, -90, -124, 97, 83, -64, -71, -77, 1, 106, -80, -10, 39, 84, -20, 11, 53, -51, -43, 106, 22, 24, 21, 103, -83, -30, -32, 98, -12, 106, 93, -90, -11, -81, 29, -67, -76, -110, 58, -36, -108, 74, -34, -14, -60, -93, -47, 24, 51, -7, 20, 30, 10, -46, 55, 89, -116, -92, 99, -76, 42, 73, -3, -45, 40, 83, 82, -106, 55, -24, -68, 16, 113, -25, 57, 108, 73, -46, -48, -78, 75, 113, 66, -54, -94, 25, -54, 87, 123, -9, -84, 5, -115, 19, 26, 51, 116, 103, -45, -16, -76, 64, -16, 32, -41, -15, -14, -30, -65, 84, 10, 31, -81, 80, -40, 88, -94, -16, -98, 32, 7, 86, -23, 4, 119, 95, 22, 87, -5, -108, 29, 23, -14, 100, 3, 15, 36, 56, -101, -66, -8, -56, 103, 108, -121, 74, 4, 22, 74, -115, 69, 55, -31, 72, 81, 6, 109, -7, 95, -44, 42, 23, -124, 101, 58, 126, -56, -61, 80, 40, -13, 98, -69, -117, 93, -35, 94, 61, -80, 127, 46, 1, -32, -18, 108, -70, 59, 112, 28, -93, -46, -123, 110, 28, 95, 56, -81, 34, 60, -29, 44, -125, -5, 18, -37, -49, -100, -39, 70, -62, 33, -71, 84, 9, -49, 121, 18, -81, -79, -10, 117, 104, 70, 38, 51, 22, 126, 125, -121, -86, -124, 59, 66, 56, 42, -57, -15, -34, -110, 78, 24, -53, -34, 67, 7, -70, -121, -25, -34, -110, -8, -46, -86, 48, -9, -20, -124, 124, 98, 116, -60, -73, -73, -66, -3, -112, -3, -79, -6, -54, -123, -49, -125, 54, 108, -10, 114, -103, -18, -8, 80, 71, 105, -55, -123, -46, 30, 27, -125, 73, 98, 41, -26, 79, -118, -19, 37, -46, 122, 71, -25, 87, -76, 101, 99, 93, -81, 90, -76, -13, -97, -58, 43, -97, 81, -44, 103, -53, 69, 35, -56, 122, 53, -92, -56, -49, 77, -23, 85, 47, -51, -11, -88, -104, -8, 103, 117, -35, 82, 127, -41, 111, -92, -97, -4, -10, -12, 3, 0, 0]}
这里没有base64。 'data' 成员只是 gzip 文件的一个字节字符串。它们存储为有符号整数,因此必须将它们转换为无符号整数才能进行处理。解压后无法识别文件格式,但里面确实有一些可读的字符串。
raw = hydata["player"]["stats"]["Pit"]["profile"]["inv_contents"]
by = bytes([i if i >= 0 else 256+i for i in raw['data']])
decoded = decompress(by)
我正在尝试解码 json 文本,然后在解码后使用 gzip 将其解压缩 但无论我如何尝试去做,我都会遇到这个错误
" File "D:\VS Code\VSC Projects\api.py", line 25, in <module>
decoded = BytesIO(decompress(b64decode(raw)))
File "C:\Users\paz-pc\AppData\Local\Programs\Python\Python39\lib\base64.py", line 80, in b64decode
s = _bytes_from_decode_data(s)
File "C:\Users\paz-pc\AppData\Local\Programs\Python\Python39\lib\base64.py", line 45, in _bytes_from_decode_data
raise TypeError("argument should be a bytes-like object or ASCII "
TypeError: argument should be a bytes-like object or ASCII string, not 'dict'
这是我的代码:
import requests
from mojang import MojangAPI
from pprint import pprint
from base64 import b64decode
from gzip import decompress
from io import BytesIO
name = str(input("Enter your name of choice >>>"))
api_key = "cant show you the key"
uuid = MojangAPI.get_uuid(name)
request_url = (str("https://api.hypixel.net/player?key=" + api_key + "&uuid=" + uuid))
hydata = requests.get(request_url).json()
raw = hydata["player"]["stats"]["Pit"]["profile"]["inv_contents"]
file1 = open("ProfessionalNames.json", 'w')
file1.write(str(raw))
file1.close()
file2 = open("ProfessionalNames.json", 'r')
decoded = BytesIO(decompress(b64decode(raw)))
pprint((decoded))
这就是 ProfessionalNames.json 中的内容,也就是我要解码 (base64) 然后解压缩 (gzip) 的内容
{'type': 0, 'data': [31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -107, 83, -37, 110, -45, 64, 16, 29, -25, -46, 38, 41, 106, -112, 0, 85, 2, -124, 70, 2, -98, 40, -120, 10, 104, -59, 99, 27, 90, 69, -94, -108, 10, 8, -81, 104, 108, 79, -20, 85, -100, -35, 104, 119, 77, 91, -15, 65, -111, -8, -116, 124, 8, -97, 2, -116, 93, 92, -110, 42, -118, 96, 95, -68, 62, 51, 103, -9, -52, -52, -39, 14, 64, 27, 2, -43, 1, -128, 71, 53, -88, -87, 56, -88, 5, -48, -20, -103, 92, -5, -96, 3, 117, 79, 73, 0, 27, 3, 29, 90, -90, 17, -123, 25, 7, 117, 104, -9, 85, -52, 71, 25, 37, 78, 72, 63, 59, -80, 30, 43, 55, -55, -24, -94, 13, -115, 99, 99, -71, 37, 104, 3, 110, -52, -90, 123, -89, 108, 71, -88, 60, -113, 5, -71, 53, -101, -66, 126, -78, -121, -5, -34, 83, 52, -62, 55, 52, -90, -124, 97, 83, -64, -71, -77, 1, 106, -80, -10, 39, 84, -20, 11, 53, -51, -43, 106, 22, 24, 21, 103, -83, -30, -32, 98, -12, 106, 93, -90, -11, -81, 29, -67, -76, -110, 58, -36, -108, 74, -34, -14, -60, -93, -47, 24, 51, -7, 20, 30, 10, -46, 55, 89, -116, -92, 99, -76, 42, 73, -3, -45, 40, 83, 82, -106, 55, -24, -68, 16, 113, -25, 57, 108, 73, -46, -48, -78, 75, 113, 66, -54, -94, 25, -54, 87, 123, -9, -84, 5, -115, 19, 26, 51, 116, 103, -45, -16, -76, 64, -16, 32, -41, -15, -14, -30, -65, 84, 10, 31, -81, 80, -40, 88, -94, -16, -98, 32, 7, 86, -23, 4, 119, 95, 22, 87, -5, -108, 29, 23, -14, 100, 3, 15, 36, 56, -101, -66, -8, -56, 103, 108, -121, 74, 4, 22, 74, -115, 69, 55, -31, 72, 81, 6, 109, -7, 95, -44, 42, 23, -124, 101, 58, 126, -56, -61, 80, 40, -13, 98, -69, -117, 93, -35, 94, 61, -80, 127, 46, 1, -32, -18, 108, -70, 59, 112, 28, -93, -46, -123, 110, 28, 95, 56, -81, 34, 60, -29, 44, -125, -5, 18, -37, -49, -100, -39, 70, -62, 33, -71, 84, 9, -49, 121, 18, -81, -79, -10, 117, 104, 70, 38, 51, 22, 126, 125, -121, -86, -124, 59, 66, 56, 42, -57, -15, -34, -110, 78, 24, -53, -34, 67, 7, -70, -121, -25, -34, -110, -8, -46, -86, 48, -9, -20, -124, 124, 98, 116, -60, -73, -73, -66, -3, -112, -3, -79, -6, -54, -123, -49, -125, 54, 108, -10, 114, -103, -18, -8, 80, 71, 105, -55, -123, -46, 30, 27, -125, 73, 98, 41, -26, 79, -118, -19, 37, -46, 122, 71, -25, 87, -76, 101, 99, 93, -81, 90, -76, -13, -97, -58, 43, -97, 81, -44, 103, -53, 69, 35, -56, 122, 53, -92, -56, -49, 77, -23, 85, 47, -51, -11, -88, -104, -8, 103, 117, -35, 82, 127, -41, 111, -92, -97, -4, -10, -12, 3, 0, 0]}
这里没有base64。 'data' 成员只是 gzip 文件的一个字节字符串。它们存储为有符号整数,因此必须将它们转换为无符号整数才能进行处理。解压后无法识别文件格式,但里面确实有一些可读的字符串。
raw = hydata["player"]["stats"]["Pit"]["profile"]["inv_contents"]
by = bytes([i if i >= 0 else 256+i for i in raw['data']])
decoded = decompress(by)