python reptile learning Weather

# - * - Coding: UTF-8 - * - 

Import urllib.request

Import json
Import gzip

cityName the INPUT = ( 'Please enter a city you want to query: \ the n-')

url # access, which is urllib.parse.quote city names are converted to component url
url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote (cityName)

# request and read weather_data
weather_data the urllib.request.urlopen = ( URL) .read ()

# encoding utf-8 in the decompressed data
weather_data = gzip.decompress (weather_data) .decode ( 'utf-8')

# json data into the data dict
weather_dict = json.loads (weather_data)

IF weather_dict.get ( 'desc') == 'invilad citykey-':
Print ( "city name input error")

elif weather_dict.get ( 'desc') == 'the OK':
Forecast = weather_dict.get ( 'Data' ) .get ( 'forecast')

startoday = 'City:' + weather_dict.get ( 'Data') GET ( 'City') + '\ n-' \.
+ 'Date:' + forecast [0] .get ( 'date') + '\ n' \
+ 'temperature:'. + weather_dict.get ( 'Data') GET ( 'wendu') + 'deg.] C \ n-' \
+ 'temperature:' + forecast [0] .get ( 'high') + '℃ \ n-'\
+' low Temperature: 'Forecast + [0] .get (' low ') +' deg.] C \ n-'\
+' direction: '+ forecast [0] .get (' fengxiang ') +' \ n '\
+ 'wind:' Forecast + [0] .get ( 'Fengli') + '\ n-' \
+ 'Weather:' Forecast + [0] .get ( "type") + "\ n-'\
+' cold: '+ weather_dict.get ('Data '.) GET (' ganmao ') +' \ n-'

one_day =' Date: 'Forecast + [. 1] .get (' DATE ') +' \ n-'\
+' Weather: '+ forecast [1]. get ( 'type') + ' \ n' \
+ 'Temperature:' Forecast + [. 1] .get ( 'High') + '\ n-' \
+ 'Low Temperature:' Forecast + [. 1] .get ( 'Low') + '\ n-' \
+ 'direction: '+ Forecast [. 1] .get (' Fengxiang ') +' \ n-'\
+' wind: '+ forecast [. 1] .get (' Fengli ') +' \ n-'

two_day =' date: '+ forecast [ 2] .get ( 'DATE') + '\ n-' \
+ 'Weather:' + forecast [2] .get ( 'type') + '\ n-' \
+ 'temperature:' + forecast [2] .get ( 'High') + '\ n-' \
+ 'low Temperature:' Forecast + [2] .get ( 'low') + '\ n-' \
+ 'direction:' + forecast [2] .get ( 'fengxiang' ) + '\ n-' \
+ 'Wind: 'Forecast + [2] .get (' Fengli ') +' \ n-'

three_day =' Date: 'Forecast + [. 3] .get (' DATE ') +' \ n-'\
+' Weather: '+ forecast [3] .get ( 'type ') + '\ n' \
+ 'Temperature:' + Forecast [. 3] .get ( 'High') + '\ n-' \
+ 'low:' Forecast + [. 3] .get ( "Low") + "\ n-'\
+' direction: 'Forecast + [. 3] .get (' Fengxiang ') +' \ n-'\
+' wind: '+ forecast [. 3] .get (' Fengli ') +' \ n-'

four_day =' date: '+ forecast [ . 4] .get ( 'DATE') + '\ n-' \
+ 'Weather:' + forecast [4] .get ( 'type') + '\ n-' \
+ 'temperature:' + forecast [4] .get ( 'High') + '\ n-' \
+ 'low Temperature:' Forecast + [. 4] .get ( 'low') + '\ n-' \
+ 'direction:' + forecast [4] .get ( 'fengxiang' ) + '\ n'\
+ '风力:' + forecast[4].get('fengli') + '\n'

print(one_day)
print(two_day)
print(three_day)
print(four_day)

Guess you like

Origin www.cnblogs.com/hfct/p/10978285.html