Crawler learning-Using the Baidu API---weather

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.