Python Learning Urllib Module---for sending network requests, getting data (5)

Source: Internet
Author: User

Check out the last section of the city weather

You need to import the results from the previous section city10.py

#!/usr/bin/python
#-*-Coding:utf-8-*-
Import Urllib.request
Import City variable name from the City10 import City #从city10. py
Import JSON #json包, usage of loads
Import Traceback


Cityname=input (' What city weather do you want to inquire about? \ n ')

Citycode=city.get (CityName) # Citycode=city[cityname]
#print (Citycode)

#print (CityName)

If Citycode:
Try
web= ' http://www.weather.com.cn/data/cityinfo/%s.html '%citycode
Content=urllib.request.urlopen (web). Read (). Decode ()
Data=json.loads (content)
#print (data)
Print (Type (content))
Print (Type (data))

result=data[' Weatherinfo ' #获取weatherinfo键的值为 {' Cityid ': ' 101010100 '}
# Just like result={}
A= ('%s\n%s~%s ')% (result[' weather '), result[' Temp1 '], result[' Temp2 '])
Print (a)
Except Exception as E:
#traceback. Print_exc ()
Print (Exception, ":", E)
Print (' Query failed ')
Else
Print (' No Find the city! ‘)

Python Learning Urllib Module---for sending network requests, getting data (5)

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.