Use python to traverse the weekly temperature of a given city

  • 2020-05-27 06:27:46
  • OfStack

In interest, wrote a weather forecast for 5 days to traverse a specified city, and turned it into a Fahrenheit display.

Write the city name to a list so you can easily add the city. With detailed notes


import requests
import json
# define 1 A function   Avoid code overwrites multiple times. 
def gettemp(week,d_or_n,date):
 wendu=data['result']['weather'][week]['info'][d_or_n][date] # Split the dictionary 
 return int(wendu)

def getft(t):
 ft=t*1.8+32
 return float(str(ft)[0:4])

cities=[' baoding ',' Beijing ',' Shanghai ',' wuhan ',' zhengzhou ',' qiqihar '] # Here you can specify the city you want to traverse 
url='http://api.avatardata.cn/Weather/Query?key=68e75677978441f6872c1106175b8673&cityname=' # Used for and cities String concatenation of the city in 
low=0
high=2
for city in cities:
 r = requests.get(url+city) #  The most basic GET request 
 #print(r.status_code)   Get the return state 200 To be successful 
 #print(r.text)  Print the decoded returned data 
 data=json.loads(r.text) # The returned json The data is converted to a dictionary type 
 #print(type(data)) data  The data type is the dictionary   So you can follow the dictionary (the list in the dictionary will follow the list) 
 print(city,' nearly 5 Weather forecast: ')
 for i in range(5):
  week=' weeks '+str(data['result']['weather'][i]['week']) # Break down the dictionary types one by one   As listing   Tuples, etc. 
  daylow=gettemp(i,'day',low)
  dlf=getft(daylow)
  dayhigh=gettemp(i,'day',high)
  dhf=getft(dayhigh)
  nightlow=gettemp(i,'night',low)
  nlf=getft(nightlow)
  nighthigh=gettemp(i,'night',high)
  nhf=getft(nighthigh)
  print(week,' Daytime temperatures :',daylow,'~',dayhigh,' c ',' At night the temperature :',nightlow,'~',nighthigh,' c ')
  print(' ',' Daytime temperatures :',dlf,'~',dhf,' Fahrenheit ',' At night the temperature :',nlf,'~',nhf,' Fahrenheit ')
 print('\n')

{"result":{"realtime":{"wind":{"windspeed":null,"direct":" West wind ","power":"3 level ","offset":null},"time":"16:00:00","weather":{"humidity":"27","img":"0","info":" sunny ","temperature":"13"},"dataUptime":"1490517362","date":"2017-03-26","city_code":"101090201","city_name":" baoding ","week":"0","moon":"2 On the 21st 9"},"life":{"date":"2017-3-26","info":{"kongtiao":[" Turn on the heating air conditioner "," You will feel a little cold, you can properly turn on the warm air conditioning to adjust the indoor temperature, so as to avoid catching a cold. "],"yundong":[" More appropriate "," The weather is good, but considering the strong wind and low temperature, you are recommended to do indoor sports, if outdoor sports pay attention to wind protection and appropriate increase or decrease clothing. "],"ziwaixian":[" medium "," Belongs to the weather of moderate intensity ultraviolet radiation, it is recommended to apply when going out SPF above 15 , PA+ Wear a hat and sunglasses. "],"ganmao":[" More prone to "," The temperature difference between day and night is large, more likely to catch a cold, please appropriate increase or decrease clothes. Weak friends please pay attention to protection. "],"xiche":[" More appropriate "," More appropriate to wash the car in the future 1 No rain, less wind, scrub 1 The new car will at least hold up 1 Days. "],"wuran":null,"chuanyi":[" cold "," Cold weather, suggest winter clothing such as cotton-padded jacket, down jacket, leather jacket and wool sweater. The elderly and infirm should wear thick cotton-padded clothes, winter coats or thick down jackets. "]}},"weather":[{"date":"2017-03-26","week":" day ","nongli":"2 On the 21st 9","info":{"dawn":null,"day":["0"," sunny ","17"," The northwest ","3-4  level ","06:12"],"night":["0"," sunny ","2"," Southwest winds "," The breeze ","18:36"]}},{"date":"2017-03-27","week":"1","nongli":"2 month 310","info":{"dawn":["0"," sunny ","2"," Southwest winds "," The breeze ","18:36"],"day":["0"," sunny ","15"," The south wind "," The breeze ","06:11"],"night":["7"," Light rain ","3"," The south wind "," The breeze ","18:37"]}},{"date":"2017-03-28","week":"2","nongli":"3 At the beginning of 1","info":{"dawn":["7"," Light rain ","3"," The south wind "," The breeze ","18:37"],"day":["1"," It's cloudy ","15"," The south wind "," The breeze ","06:09"],"night":["0"," sunny ","3"," The south wind "," The breeze ","18:38"]}},{"date":"2017-03-29","week":"3","nongli":"3 At the beginning of 2","info":{"dawn":["0"," sunny ","3"," The south wind "," The breeze ","18:38"],"day":["0"," sunny ","18"," The south wind "," The breeze ","06:08"],"night":["0"," sunny ","3"," The north wind "," The breeze ","18:39"]}},{"date":"2017-03-30","week":"4","nongli":"3 At the beginning of 3","info":{"dawn":["0"," sunny ","3"," The north wind "," The breeze ","18:39"],"day":["0"," sunny ","17"," The north wind "," The breeze ","06:06"],"night":["0"," sunny ","3"," The north wind "," The breeze ","18:40"]}}],"pm25":{"key":"Baoding","show_desc":"0","pm25":{"curPm":"34","pm25":"14","pm10":"26","level":"1","quality":" optimal ","des":" The air is good enough to go out "},"dateTime":"2017 years 03 month 26 day 16 when ","cityName":" baoding "},"isForeign":0},"error_code":0,"reason":"Succes"}
 This is the return 1 a json Data can be passed json Formatting tools are easy to view 1 Some, json.loads It's all dictionary lists 1 Some nested data that you want to fetch   In the dictionary "result" In,   while data['result']  again 1 A dictionary, 
{'life': {'date': '2017-3-26', 'info': {'yundong': [' More appropriate ', ' The weather is good, but considering the strong wind and low temperature, you are recommended to do indoor sports, if outdoor sports pay attention to wind protection and appropriate increase or decrease clothing. '], 'xiche': [' More appropriate ', ' More appropriate to wash the car in the future 1 No rain, less wind, scrub 1 The new car will at least hold up 1 Days. '], 'ganmao': [' More prone to ', ' The temperature difference between day and night is large, more likely to catch a cold, please appropriate increase or decrease clothes. Weak friends please pay attention to protection. '], 'ziwaixian': [' medium ', ' Belongs to the weather of moderate intensity ultraviolet radiation, it is recommended to apply when going out SPF above 15 , PA+ Wear a hat and sunglasses. '], 'chuanyi': [' cold ', ' Cold weather, suggest winter clothing such as cotton-padded jacket, down jacket, leather jacket and wool sweater. The elderly and infirm should wear thick cotton-padded clothes, winter coats or thick down jackets. '], 'wuran': None, 'kongtiao': [' Turn on the heating air conditioner ', ' You will feel a little cold, you can properly turn on the warm air conditioning to adjust the indoor temperature, so as to avoid catching a cold. ']}}, 'weather': [{'date': '2017-03-26', 'week': ' day ', 'info': {'dawn': None, 'night': ['0', ' sunny ', '2', ' Southwest winds ', ' The breeze ', '18:36'], 'day': ['0', ' sunny ', '17', ' The northwest ', '3-4  level ', '06:12']}, 'nongli': '2 On the 21st 9'}, {'date': '2017-03-27', 'week': '1', 'info': {'dawn': ['0', ' sunny ', '2', ' Southwest winds ', ' The breeze ', '18:36'], 'night': ['7', ' Light rain ', '3', ' The south wind ', ' The breeze ', '18:37'], 'day': ['0', ' sunny ', '15', ' The south wind ', ' The breeze ', '06:11']}, 'nongli': '2 month 310'}, {'date': '2017-03-28', 'week': '2', 'info': {'dawn': ['7', ' Light rain ', '3', ' The south wind ', ' The breeze ', '18:37'], 'night': ['0', ' sunny ', '3', ' The south wind ', ' The breeze ', '18:38'], 'day': ['1', ' It's cloudy ', '15', ' The south wind ', ' The breeze ', '06:09']}, 'nongli': '3 At the beginning of 1'}, {'date': '2017-03-29', 'week': '3', 'info': {'dawn': ['0', ' sunny ', '3', ' The south wind ', ' The breeze ', '18:38'], 'night': ['0', ' sunny ', '3', ' The north wind ', ' The breeze ', '18:39'], 'day': ['0', ' sunny ', '18', ' The south wind ', ' The breeze ', '06:08']}, 'nongli': '3 At the beginning of 2'}, {'date': '2017-03-30', 'week': '4', 'info': {'dawn': ['0', ' sunny ', '3', ' The north wind ', ' The breeze ', '18:39'], 'night': ['0', ' sunny ', '3', ' The north wind ', ' The breeze ', '18:40'], 'day': ['0', ' sunny ', '17', ' The north wind ', ' The breeze ', '06:06']}, 'nongli': '3 At the beginning of 3'}], 'isForeign': 0, 'pm25': {'pm25': {'des': ' The air is good enough to go out ', 'curPm': '34', 'level': '1', 'pm10': '26', 'pm25': '14', 'quality': ' optimal '}, 'show_desc': '0', 'key': 'Baoding', 'dateTime': '2017 years 03 month 26 day 16 when ', 'cityName': ' baoding '}, 'realtime': {'city_name': ' baoding ', 'weather': {'info': ' sunny ', 'img': '0', 'humidity': '27', 'temperature': '13'}, 'week': '0', 'wind': {'windspeed': None, 'power': '3 level ', 'offset': None, 'direct': ' West wind '}, 'city_code': '101090201', 'date': '2017-03-26', 'dataUptime': '1490517362', 'time': '16:00:00', 'moon': '2 On the 21st 9'}}
 Let's do it the same way  data['result']['weather']  This is another 1 A tuple, 
[{'nongli': '2 On the 21st 9', 'info': {'night': ['0', ' sunny ', '2', ' Southwest winds ', ' The breeze ', '18:36'], 'dawn': None, 'day': ['0', ' sunny ', '17', ' The northwest ', '3-4  level ', '06:12']}, 'week': ' day ', 'date': '2017-03-26'}, {'nongli': '2 month 310', 'info': {'night': ['7', ' Light rain ', '3', ' The south wind ', ' The breeze ', '18:37'], 'dawn': ['0', ' sunny ', '2', ' Southwest winds ', ' The breeze ', '18:36'], 'day': ['0', ' sunny ', '15', ' The south wind ', ' The breeze ', '06:11']}, 'week': '1', 'date': '2017-03-27'}, {'nongli': '3 At the beginning of 1', 'info': {'night': ['0', ' sunny ', '3', ' The south wind ', ' The breeze ', '18:38'], 'dawn': ['7', ' Light rain ', '3', ' The south wind ', ' The breeze ', '18:37'], 'day': ['1', ' It's cloudy ', '15', ' The south wind ', ' The breeze ', '06:09']}, 'week': '2', 'date': '2017-03-28'}, {'nongli': '3 At the beginning of 2', 'info': {'night': ['0', ' sunny ', '3', ' The north wind ', ' The breeze ', '18:39'], 'dawn': ['0', ' sunny ', '3', ' The south wind ', ' The breeze ', '18:38'], 'day': ['0', ' sunny ', '18', ' The south wind ', ' The breeze ', '06:08']}, 'week': '3', 'date': '2017-03-29'}, {'nongli': '3 At the beginning of 3', 'info': {'night': ['0', ' sunny ', '3', ' The north wind ', ' The breeze ', '18:40'], 'dawn': ['0', ' sunny ', '3', ' The north wind ', ' The breeze ', '18:39'], 'day': ['0', ' sunny ', '17', ' The north wind ', ' The breeze ', '06:06']}, 'week': '4', 'date': '2017-03-30'}]
 Then take the dictionary in the tuple and gradually split it to get the desired data. 

Related articles: