4.Write the code line by line:
Step 1. requests
Do what the
docs: tell me to do.
By the way, I love the way this doc wrote!! It's so funny! I read it with a BIG BIG BIG laugh!
Warning: Recreational use of other HTTP libraries may result in dangerous side-effects, including: security vulnerabilities, verbose code, reinventing the wheel, constantly reading documentation, depression, headaches, or even death.
(1) To get the API KEY from openweathermap.com
key = 90cb9d98ac5f5c13cc6c2ab80ab5a024 url = api.openweathermap.org/data/2.5/weather?q={city name}
(2)then def the requests,use JSON parameter to encoding the dict.That's the important part,to fetch API from OpenWeatherMap.com,and deal with the 'Response' data, which is set by 'JSON' form.
According to the 'Requests' office file ,and "API documentation" from OWM.com,
the Data Processing Module Function should be that way:
def fetchAPI(city):
url = 'http://api.openweathermap.org/data/2.5/weather' payload = {'APPID':'90cb9d98ac5f5c13cc6c2ab80ab5a024','q':city, 'lang':'zh_cn'}
step 2: Status
I can't tell the different from them, just chose one at random:
if "status" in data:
return 'sorry,please try again.'
and
if r.status_code != requests.codes.ok:
return 'sorry,please try again.'
step 3: API
Read the data of API respond to know the format of the data. I want to extract the 'weather'/'temp'/'wind' from them. Forget me, I think Less is more...
{..."weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}], "main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37 },
"wind":{"speed":5.1,"deg":150},...}
So,there is the nested format('嵌套' in chinese?),the whole respond is in the format of dict,value = data['something you want'],you have to think it very carefully.
The format of "weather" is list,there is a dict inside this list, the dict only has one element,so use the integer indices [0] to get the value of the key'description';
The format of "temp" and "wind" are dict,the datas are the value of the keys "temp"and "speed",so use the key to get the value.
Anyway:it should be wrote like this:
weather = data['weather'][0]['description']
temp = str(data['main']['temp'])+'℉'
wind = str(data['wind']['speed']) + 'miles/hour'
onlinetime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
result = city, weather, temp, wind
print(result)
return onlinetime, weather, temp, wind
Here,whiling writing the "return"and "print"function,"I made a big mistake , I will mention it in next section.
step 4:
To manipulate the "input" variables,and connect it to the "fetchAPI(city)" function.
I fell in the last step ... Thanks to simpleowen(大猫)! He saved me,again!! Do not be afraid of python for simpleowen with you and will rescue you declares the Lord. Well, I wish next time I can complete all the tasks individually.
while True:
m = input('You can enter city name in Chinese or English:')
if m == 'help':
print('''
# omitted
''')
elif m == "history":
if len(searchHistory) == 0:
print("No history records.")
else:
for i in searchHistory:
print(i)
searchHistory.append(m)
elif m == "quit":
print('Goodbye!')
quit()
else:
fetchAPI(m) # here I fell down
Summary:
I may envy the classmates that use OOP to finish the CH3 task,but not jealous. Due to my weak foundation of programming, I know how hard it will be.
In next section, I will show you my learning process of ch3. In general, the phenomenon of "much time but low efficiency" occured, again. (What else can I say?)