从请求获取字节响应

时间:2018-12-26 16:59:46

标签: python json python-3.x request

我正在尝试在Pyhton 3上执行一个请求,该请求应返回一个JSON。相反,它返回的是我无法转换的字节序列。为什么我会收到这种类型的响应,如何将其转换为人类可读的数据?

下面是我的代码段:

headers = {}
headers['Host']= 'XXXXX' # hidden
headers['Connection']= 'keep-alive'
headers['Content-Length']= '122'
headers['Accept']= 'application/json, text/javascript, */*; q=0.01'
headers['Origin']= 'XXXXX' # hidden
headers['X-Requested-With']= 'XMLHttpRequest'
headers['User-Agent']= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
headers['Content-Type']= 'application/json'
headers['Referer']= 'XXXXX' # hidden
headers['Accept-Encoding']= 'gzip, deflate, br'
headers['Accept-Language']= 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7'
headers['Cookie'] = 'XXXXX' # hidden

try:

    req = request.Request(url,post_data,headers)
    x = request.urlopen(req)
    print(x.read())
    print(x.info())

except Exception as e:
    print(e)

以下收到的回复:

    b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03L\x8fAK\x031\x10\x85\xff\xca0\x07Q\x88\x899(\xb2\xd0\x93\xf4\xe2\xa1-z]\x90\xecf\xb6\x1b\xd9d\xca$-H\xe9\x7f7\x91\x8a^\x86\x997\xef\x1b\xde\x9c\xf1D\x92\x03\'\xec\xd0j\x8b\nI\x84\x05\xbb\xf3_\x13)g\xb7\xa7\xea\x88n\x99X"yx}\xdfn \x17\ti\xaf Q(3\t8\x11\xf7\xa5\x80\x87O\x1aK\x95\x8fq QW\x1bp5\x14\x8e\xaaV\x18g\'n,\x95\xe1i\xcaT\xe0\x01n\x07\xaa\xb7\t\xfa\xdfD\xab\x9a\xe7&R\x99\xd9\xaf\xd6Z\xeb\x1e\xef\x1aj\x8eYL\xae<\x99\x03\xc9\xf2hN\x94<\xcbG\x1bL\x8b\xa5\x0f\x11\x96\x90\x08\xec\xd3\xb3\xee\x13^\x14&\x17[\xfc\xb6}\xdb\xbd\xac\x7f\x1eS\xff\xfe\xda9\xc9\x04t\xd5G\xf6M\xb4\x8d\x0c\x1e\xbb{{\xf9\x06\x00\x00\xff\xff\x03\x00\xc4\xd9gg\'\x01\x00\x00'
Date: Wed, 26 Dec 2018 16:46:48 GMT
Server: Apache
Strict-Transport-Security: max-age=16070400
X-UA-Compatible: IE=Edge,chrome=1, IE=Edge,chrome=1
Content-Type: application/json; charset=utf-8
Vary: Accept-Encoding
Content-Encoding: gzip
X-Frame-Options: SAMEORIGIN
Connection: close
Transfer-Encoding: chunked

2 个答案:

答案 0 :(得分:1)

尝试这样的事情

import requests

r = requests.post('your URL',data=YourData)
r.json()

答案 1 :(得分:1)

似乎已压缩:public loadData() { this.exampleDatabase = new DataService(this.httpClient); this.exampleDatabase.getAllIssues().subscribe(data => { this.dataSource = new MatTableDataSource(data); this.dataSource.data = data; this.dataLength = data.length; this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

解压缩它,然后使用/** * Paints the border for the specified component with the specified * position and size. */ @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { // fake drop shadow effect in case of heavy weight popups JComponent popup = (JComponent) c; Image hShadowBg = (Image) popup.getClientProperty(ShadowPopupFactory.PROP_HORIZONTAL_BACKGROUND); if (hShadowBg != null) { g.drawImage(hShadowBg, x, y + height - 5, c); } Image vShadowBg = (Image) popup.getClientProperty(ShadowPopupFactory.PROP_VERTICAL_BACKGROUND); if (vShadowBg != null) { g.drawImage(vShadowBg, x + width - 5, y, c); } // draw drop shadow g.drawImage(shadow, x + 5, y + height - 5, x + 10, y + height, 0, 6, 5, 11, null, c); g.drawImage(shadow, x + 10, y + height - 5, x + width - 5, y + height, 5, 6, 6, 11, null, c); g.drawImage(shadow, x + width - 5, y + 5, x + width, y + 10, 6, 0, 11, 5, null, c); g.drawImage(shadow, x + width - 5, y + 10, x + width, y + height - 5, 6, 5, 11, 6, null, c); g.drawImage(shadow, x + width - 5, y + height - 5, x + width, y + height, 6, 6, 11, 11, null, c); }

示例:

Content-Encoding: gzip

另一个选择-告诉服务器您对压缩内容感到不满。从json.decode请求标头中删除import zlib decompressed_data=zlib.decompress(f.read(), 16+zlib.MAX_WBITS) 和其他可能的压缩类型