用python编写的获取天气预报的代码总是有错误,求解
importrequestsApiUrl=\"http://www.weather.com.cn/adat/cityinfo/101010100.html"r=reque...
import requests
ApiUrl=\
"http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json
print (weatherinfo["ptime"]["temp2"])
在python3.0显示
Traceback (most recent call last):
File "/home/pi/Desktop/5.py", line 6, in <module>
print (weatherinfo["ptime"]["temp2"])
TypeError: 'method' object is not subscriptable
而在python2.0显示
Traceback (most recent call last):
File "/home/pi/Desktop/7.py", line 6, in <module>
print weatherinfo["ptime"]["temp2"]
TypeError: 'instancemethod' object has no attribute '__getitem__'
求解,本人没有学过python只学过c,c++。。 展开
ApiUrl=\
"http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json
print (weatherinfo["ptime"]["temp2"])
在python3.0显示
Traceback (most recent call last):
File "/home/pi/Desktop/5.py", line 6, in <module>
print (weatherinfo["ptime"]["temp2"])
TypeError: 'method' object is not subscriptable
而在python2.0显示
Traceback (most recent call last):
File "/home/pi/Desktop/7.py", line 6, in <module>
print weatherinfo["ptime"]["temp2"]
TypeError: 'instancemethod' object has no attribute '__getitem__'
求解,本人没有学过python只学过c,c++。。 展开
展开全部
weatherinfo=r.json() #在json后面加上括号才能返回结果。否则只能返回函数地址。
以下python3通过:
import requests
ApiUrl="http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json()
print (weatherinfo["weatherinfo"]["ptime"])
print (weatherinfo["weatherinfo"]["temp2"])
>>>08:00
>>>5℃
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询