Python请求失败但邮递员返回200

时间:2017-11-21 05:14:30

标签: python

我的请求API调用有效,因为它返回200,但无法加载JSON对象。

import requests

xvg_wallet = 'http://api.yiimp.eu/api/wallet?address=DLGuHUwkycBFFajtbMJwMqvcgZ6cVDebCY'
w = requests.get(xvg_wallet).json()
print(w)

Traceback (most recent call last):
  File "C:\Users\Brett\Downloads\verge.py", line 4, in <module>
    w = requests.get(xvg_wallet).json()
  File "C:\Users\Brett\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\models.py", line 826, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Brett\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Brett\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Brett\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

然而,与Postman应用程序相同的GET只会返回成功。

enter image description here

我在使用Python做错了什么?

1 个答案:

答案 0 :(得分:3)

您只需在请求时包含标题,请参阅以下代码:

import requests

xvg_wallet = 'http://api.yiimp.eu/api/walletaddress=DLGuHUwkycBFFajtbMJwMqvcgZ6cVDebCY'

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
w = requests.get(xvg_wallet, headers=headers).json()
print(w)

输出:

{'balance': 0.0,
 'currency': 'XVG',
 'paid24h': 65.10781097,
 'total': 68.29277167,
 'unpaid': 3.1849607,
 'unsold': 3.1849607040682}