Section 2: Fell in the last step
I hate to say but have to admit, this section is meaningless. It just my another stupid record about learning python. Don't waste your time to read it.I wrote this just to cure myself, since being honest is the characteristic of me.
BEHOLD, how I struggled for requests/API/JSON ~
1.How to kill a person while "return" before "print"?
First I want to show you the last straw. It was so heavy and vague,but turned to simple and pure,after simpleowen(大猫) pointed out to me.
There are the codes I wrote:
return onlinetime, weather, temp, wind
print(result)
and :
else:
print(result)
It was so ashamed to display such a simple solution to my question:
simpleowen(大猫):
1.while Ture里面没有调用fetchAPI; 2.fetchAPI返回以后再打印是打印不出来的; 3.return语句后面的语句不会执行
HERE YOU ARE:
print(result) # here
return onlinetime, weather, temp, wind
AND
else:
fetchAPI(m)
So, just one minute !! It works!! It had already cost me almost one day try to find out the bug(and failed)!! Shoot me!! With a "return",and then "print" me, please...
2. How to send a APIKEY?
BGM: 张士超你昨天晚上到底把我家钥匙放在哪里了?
I read the office docs of openweathermap.com, try to know How to get and How to use a API key (APPID).
API call: http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID={APIKEY} Parameters: APPID {APIKEY} is your unique API key Example of API call: api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111
Hello! That's not enough!! I need more information!! What the hell is "city?id=524901"?? Dose that mean cityid is also a variable? Do I need to send it, how?
and the docs of JSON:
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload) print(r.text) { ... "form": { "key2": "value2", "key1": "value1" }, ... }
How should I do to relate the APIKEY and JSON and the INPUT??Dose input equal the city or cityname? Should I use "payload" or not? Does "location" is necessary? Why I can see it everywhere? Why there is "cityID"? I don't want cityID, I don't want something new.
Anyway, I tried this:
def fetchAPI(citynadef fetchAPI(cityname):me):
url = api.openweathermap.org/data/2.5/weather?q={cityname}
payload = {'cityname':'weadata','key':'90cb9d98ac5f5c13cc6c2ab80ab5a024'}
r = requests.get(url,params=payload)
r.text # or req.json()?? I am not sure.
And then it failed. Of course!! I don't know exactly how to do with APIKEY and the mysterious 'q',does the 'payload' necessary? How about the "r.text",does it need to be "json.()" or json.loads()" ? Can I just write:
def fetchAPI(cityname):
url = 'http://api.openweathermap.org/data/2.5/weather?q=%s &APPID=90cb9d98ac5f5c13cc6c2ab80ab5a024'% cityname
r = requests.get(url,params=cityname)
NO,it failed again. I looked over the codes of classmates,but most of them use the '心知天气',not OWM. OK, just a moment, registered for the APIKEY, and then read the file of THINKPAGE.And I still confused about that. Read the 'Requests' office file ,and "API documentation" from OWM.com for the second time, still, I got stcuk. Come on, I just want to call current weather data for one location,WHY everythin be so hard to me!
Let me try again:
API call: api.openweathermap.org/data/2.5/weather?q={city name} Parameters: q city name and country code divided by comma. Examples of API calls: api.openweathermap.org/data/2.5/weather?q=London
SO the url should be:'http://api.openweathermap.org/data/2.5/weather' and "payload" should be used.(actually I am not sure) Finally I did it in this way:
def fetchAPI(city):
url = 'http://api.openweathermap.org/data/2.5/weather'
payload = {'APPID':'90cb9d98ac5f5c13cc6c2ab80ab5a024','q':city, 'lang':'zh_cn'}
r = requests.get(url, params=payload)
data = json.loads(r.text)
Maybe it will be continued later...