- 帖子
- 2
- 精华
- 0
- 积分
- 9
- 阅读权限
- 10
- 注册时间
- 2020-5-18
- 最后登录
- 2020-5-19
|
import requests
while True:
city=input('请输入城市,回车退出:\n')
if not city:
break
req=requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%s' % city)
print(req.text)
dic_city=req.json
print(dic_city)
print(type(req.text))
print(type(req.json))
city_data=dic_city.get('data')
print(city_data)
if city_data:
city_forecast=city_data['forecast'][0]
print(city_forecast.get('date'))
print(city_forecast.get('high'))
print(city_forecast.get('low'))
print(city_forecast.get('type'))
else:
print('未获得')
以上是我跟着博主教程输入的代码,但是结果一直出不来。结果如下:
请输入城市,回车退出:北京{"data":{"yesterday":{"date":"17日星期日","high":"高温 23℃","fx":"西北风","low":"低温 13℃","fl":"<![CDATA[2级]]>","type":"多云"},"city":"北京","forecast":[{"date":"18日星期一","high":"高温 26℃","fengli":"<![CDATA[3级]]>","low":"低温 15℃","fengxiang":"北风","type":"多云"},{"date":"19日星期二","high":"高温 29℃","fengli":"<![CDATA[2级]]>","low":"低温 14℃","fengxiang":"西北风","type":"晴"},{"date":"20日星期三","high":"高温 28℃","fengli":"<![CDATA[2级]]>","low":"低温 14℃","fengxiang":"东风","type":"多云"},{"date":"21日星期四","high":"高温 26℃","fengli":"<![CDATA[2级]]>","low":"低温 14℃","fengxiang":"东风","type":"多云"},{"date":"22日星期五","high":"高温 30℃","fengli":"<![CDATA[2级]]>","low":"低温 17℃","fengxiang":"东南风","type":"晴"}],"ganmao":"感冒易发期,外出请适当调整衣物,注意补充水分。","wendu":"17"},"status":1000,"desc":"OK"}<bound method Response.json of <Response [200]>><class 'str'><class 'method'>
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-360-ee31cab28f88> in <module> 10 print(type(req.text)) 11 print(type(req.json))---> 12 city_data=dic_city.get('data') 13 print(city_data) 14 if city_data:AttributeError: 'function' object has no attribute 'get'
把get换成key也没解决到,请大神帮个忙啊
|
|