Crossin的编程教室
标题:
调用天气接口
[打印本页]
作者:
Empty丶
时间:
2018-6-6 14:07
标题:
调用天气接口
city_name =
raw_input
(
'请输入要查询的城市:
\n
'
)
address =
'https://www.sojson.com/open/api/weather/json.shtml?city=%s'
%(cityname)
url = (address)
city_info = urllib2.urlopen(url).read()
print
city_info[
'data'
][
'quality'
]
Traceback (most recent call last):
File "F:/PythonWorkSpace/firstPython/citycode.py", line 151, in <module>
print dict(
city_info
['data']['quality'])
TypeError: string indices must be integers, not str
print
city_info
QQ截图20180606140430.png
(87.46 KB, 下载次数: 363)
2018-6-6 14:06 上传
点击文件名下载附件
print city_info
作者:
Empty丶
时间:
2018-6-6 14:11
city_info = {
"date": "20180606",
"message": "Success !",
"status": 200,
"city": "上海",
"count": 1225,
"data": {
"shidu": "75%",
"pm25": 81.0,
"pm10": 84.0,
"quality": "轻度污染",
"wendu": "23",
"ganmao": "儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼",
"yesterday": {
"date": "05日星期二",
"sunrise": "04:50",
"high": "高温 27.0℃",
"low": "低温 21.0℃",
"sunset": "18:55",
"aqi": 124.0,
"fx": "西南风",
"fl": "<3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}
}
}
print type(city_info)
print city_info['data']['quality']
代码这样写,是可以取到quality的值。但直接从接口那取的就不行。。。求解。。。
作者:
crossin先生
时间:
2018-6-6 17:17
直接接口拿到的是字符串不是dict
要转也是用 json.loads 转
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/)
Powered by Discuz! X2.5