Crossin的编程教室

标题: 关于提取“北京”天气信息时遇到的问题 [打印本页]

作者: gaofan    时间: 2020-5-18 14:10
标题: 关于提取“北京”天气信息时遇到的问题
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也没解决到,请大神帮个忙啊





作者: crossin先生    时间: 2020-5-18 14:14
dic_city=req.json()

少了括号
作者: gaofan    时间: 2020-5-19 15:07
crossin先生 发表于 2020-5-18 14:14
dic_city=req.json()

少了括号

哇,感谢感谢
作者: 落木千山天远大    时间: 2020-8-27 22:30
这里的if city_data:
        city_forecast=city_data['forecast'][0]
是什么意思啊,if后面不应该是跟条件吗?这里是如果city_data存在就执行下面语句?
city_forecast=city_data['forecast'][0]是因为这个是字典里面套字典?data字典里value为forecast的值?那为什么后面还有个【0】呀?
作者: crossin先生    时间: 2020-8-28 19:13
落木千山天远大 发表于 2020-8-27 22:30
这里的if city_data:
        city_forecast=city_data['forecast'][0]
是什么意思啊,if后面不应该是跟条 ...

这里是如果city_data存在就执行下面语句?
--------------
是的,相当于 if bool(city_data) != False:


city_forecast=city_data['forecast'][0]是因为这个是字典里面套字典?data字典里value为forecast的值?那为什么后面还有个【0】呀?
---------------
字典的一个元素是列表,相当于
v = city_data['forecast']  # 得到一个列表
city_forecast = v[0]  # 列表的第一项

可以把city_data输出出来观察下

作者: 落木千山天远大    时间: 2020-8-30 10:54
crossin先生 发表于 2020-8-28 19:13
这里是如果city_data存在就执行下面语句?
--------------
是的,相当于 if bool(city_data) != False:

感谢!!!!!




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5