我正试图向我的交易账户发出网络请求。python无法解码web请求。Web请求成功,代码为-200。
下面是代码

import requests

headers = {
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    'x-kite-version': '1.2.1',
    'accept': 'application/json, text/plain, */*',
    'referer': 'https://kite.zerodha.com/orders',
    'authority': 'kite.zerodha.com',
    'cookie': '__cfduid=db8fb54c76c53442fb672dee32ed58aeb1521962031; _ga=GA1.2.1516103745.1522000590; _gid=GA1.2.581693731.1522462921; kfsession=CfawFIZq2T6SghlCd8FZegqFjNIKCYuO; public_token=7FyfBbbxhiRRUso3425TViK2VmVszMCK; user_id=XE4670',
    'x-csrftoken': '7FyfBbbxhiRRUso3425TViK2VmVszMCK',
}

response = requests.get('https://kite.zerodha.com/api/orders', headers=headers)


x=str(response.content.decode("utf-8") )

B“1x\x14\x00\xfe\xa7\x9b\xd3\xca\xbd9-\x12\x83\xbfuls1\x1d8\x9d\x0e\xd4\xcf\xbd\xb8\xd1\xbd4\xc0\x00\x13~\x94}\xe4\x81\xa4\x90p\x1cfs\xcd\x1e\xaeg\x9b},m\xbd\t\x84l1\xde\xde\XXX8e\X8a\xx1h\x0e\x0c\x0c\x11\x12\xfb\x06z\xec\x18\xe4r\x11\x1c\x11\xe8\xbcco\xec\xe2;\xa6\x90\xa9\xd9\xdf2\xf2\xx11\xfa\xfa\xf3\x1e\x04\x0e\xa2\x8d\xx22\xxxxxx\xbd\xbd\xbd\xbd\xbd\xbd\xbd\xbd\xbd\xbd\xbd X08\x85L\r\x0cy\xf8\x81d;\x92!o\xfd\xbd\xe3u>3\x10\xe1\x8c;\xb8\x9e\xcea\xae\x0exx\xc9\x19s\xeb\xe5r~1\x98\xed0\xb8\xdc\xb4\x17:\x14\x96xan\xb9\xf0\xce\xf2l\\xa6g?5o\x9b\xf3\xc1\\x1f\x0f\x8fs\x1b/\x17\x1a\x0c[ysax\x1d'\xe7\xbb\nx\xacr~\xbb\x9f\xe0\x8c?S\xc0\x8f\xe0\x97\xff\xde'\xc7 \x8f\x97\xaf\xaa%\xf2\xf9\xfac \xcf\t\xf3\xeb\xaa\xdcs\xcc\xf5\xa3rm\xbaoy\xf5\x9fe\xfc\x07\xff\x01“
无法解码。在Stakoverflow上尝试了Unicode-UTF 8和各种可用的代码,但失败了。

最佳答案

根据response.headers(您没有提供,但可以通过运行代码轻松恢复),响应使用Brotli compressionContent-Encoding': 'br')进行编码。你可以用brotlipy减压:

import brotli
brotli.decompress(response.content)
#b'{"status":"success","data":[{"placed_by":"XE4670","order_id":"180331000000385",
#"exchange_order_id":null,"parent_order_id":null,"status":"REJECTED",
#"status_message":"ADAPTER is down","order_timestamp":"2018-03-31 07:59:42",
#"exchange_update_timestamp":null,...}

现在,它是json,正如承诺的那样('Content-Type': 'application/json')。

09-08 02:19