我一直在编写一个从在线API中提取信息的应用程序,我需要一些帮助。
我正在使用请求,我目前的代码如下
myData = requests.get('theapiwebsitehere.com/thispartisworking')
myRealData = myData.json()
x = myRealData['data']['playerStatSummaries']['playerStatSummarySet']['maxRating']
print x
然后我收到此错误
myRealData = myData.json()
TypeError: 'NoneType' object is not callable
我希望能够获得变量maxRating,并将其打印出来,但我似乎无法做到这一点。
感谢您的帮助。
答案 0 :(得分:22)
首先,确保使用最新版本的requests
(其1.1.0);在以前的版本中,json
不是方法,而是属性。
>>> r = requests.get('https://api.github.com/users/burhankhalid')
>>> r.json['name']
u'Burhan Khalid'
>>> requests.__version__
'0.12.1'
在最新版本中:
>>> import requests
>>> requests.__version__
'1.1.0'
>>> r = requests.get('https://api.github.com/users/burhankhalid')
>>> r.json()['name']
u'Burhan Khalid'
>>> r.json
<bound method Response.json of <Response [200]>>
但是,你得到的错误是因为你的URL没有返回有效的json,而你试图调用None
,该属性返回的内容:
>>> r = requests.get('http://www.google.com/')
>>> r.json # Note, this returns None
>>> r.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
总结:
requests
(pip install -U requests
)答案 1 :(得分:0)
首先是myData实际上返回了什么?
如果是,那么你可以尝试以下方法,而不是使用.json()函数
导入Json包并在文本上使用Json加载函数。
import json
newdata = json.loads(myData.text())