设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 5888|回复: 1
打印 上一主题 下一主题

关于提取“北京”天气信息时遇到的问题

[复制链接]

1

主题

0

好友

9

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2020-5-18 14:10:36 |显示全部楼层 |倒序浏览
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也没解决到,请大神帮个忙啊




回复

使用道具 举报

1

主题

0

好友

9

积分

新手上路

Rank: 1

沙发
发表于 2020-5-19 15:07:17 |显示全部楼层
crossin先生 发表于 2020-5-18 14:14
dic_city=req.json()

少了括号

哇,感谢感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-7 22:11 , Processed in 0.016141 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部