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)