#coding: Utf-8
#version: 0.1
#note: The API can query the air Quality index of a designated city, but the number of cities is limited, as of March 26, 2015, can only be found in 161 cities nationwide.
Import Urllib.request
Import JSON
Import Collections
Import Urllib.parse
url = "Http://apistore.baidu.com/microservice/aqi?city="
City = input ("Enter the cities you want to query:")
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格式, and specified as an ordered dictionary, converted to the list format? And keep it orderly, can not add, preferably plus
if (info[' errnum ') = =-1): #查找失败
Print (info[' errmsg ')
else: #输出天气相关信息
Print ("The City Air quality index you are querying is as follows:")
Print ("City:", info[' retdata ' [' Cities '])
Print ("Acquisition Time:", info[' retdata ' [']])
Print ("Air Quality Index:", info[' retdata ' [' AQI '])
Print ("Air rating:", info[' retdata ' [' Level '])
Print ("Primary pollutant:", info[' retdata ' [' Core '])
Crawler learning-Using the Baidu API---weather