码迷,mamicode.com
首页 > Windows程序 > 详细

爬虫学习--使用百度api---天气

时间:2015-05-16 13:23:30      阅读:727      评论:0      收藏:0      [点我收藏+]

标签:

#coding:utf-8
#version:0.1
#note:该即用API能查询指定城市的空气质量指数,但城市数量有限,截止2015年3月26日,只能查到全国161个城市的。

 

import urllib.request
import json
import collections
import urllib.parse

url = "http://apistore.baidu.com/microservice/aqi?city="

city = input("输入你想查询的城市:")
city = urllib.parse.quote(city)

url = url + city #完整的URL
result = urllib.request.urlopen(url).read().decode("utf-8")
info = json.loads(result,object_pairs_hook=collections.OrderedDict) #json格式转换为python格式,并指定为有序字典,转化成了list格式?并保持有序,可以不加上,最好加上

 

if (info[‘errNum‘] == -1): #查找失败
print(info[‘errMsg‘])
else: #输出天气相关信息
print("你查询的城市空气质量指数如下:")
print("城市:", info[‘retData‘][‘city‘])
print("采集时间:", info[‘retData‘][‘time‘])
print("空气质量指数:", info[‘retData‘][‘aqi‘])
print("空气等级:", info[‘retData‘][‘level‘])
print("首要污染物:", info[‘retData‘][‘core‘])

爬虫学习--使用百度api---天气

标签:

原文地址:http://www.cnblogs.com/my-time/p/4507679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!