首頁 > 後端開發 > Python教學 > 帶有 Oauth1 的雅虎天氣 API

帶有 Oauth1 的雅虎天氣 API

PHP中文网
發布: 2017-07-11 18:13:23
原創
1787 人瀏覽過

Yahoo重新开放了天气API,不使用oauth只能每天获取2000次/ip

使用oauth获取天气的python代码如下,使用了requests_oauthlib进行认证

使用oauth获取天气的次数为每小时2w次,每天10w次。

使用前请注册一个Yahoo的APP获得key

# sudo pip install requests requests_oauthlib

import requests
from requests_oauthlib import OAuth1

consumerKey = <Your Consumer Key from Yahoo>
consumerSecret = <Your Consumer Secret from Yahoo>

baseurl = "https://query.yahooapis.com/v1/yql?"
yql_query = 'select * ' + 'from weather.forecast where woeid in'
yql_query += ' (select woeid from geo.places(1) where text="%s") and u="c"' % "Beijing"

auth = OAuth1(consumerKey, consumerSecret)

result = requests.post(baseurl, data={'q': yql_query, 'format': 'json'}, auth=auth)

print result

# Out[10]: <Response [200]>

print result.json()<br><br># Out[11]: u'{"query":{"count":1,"created":"2017-07-10T12:50:30Z","lang":"en-US","results":{"channel":{"units":{"distance":"km","pressure":"mb","speed":"km/h","temperature":"C"},"title":"Yahoo! Weather - Beijing, Beijing, CN","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*","description":"Yahoo! Weather for Beijing, Beijing, CN","language":"en-us","lastBuildDate":"Mon, 10 Jul 2017 08:50 PM CST","ttl":"60","location":{"city":"Beijing","country":"China","region":" Beijing"},"wind":{"chill":"90","direction":"205","speed":"17.70"},"atmosphere":{"humidity":"41","pressure":"33626.85","rising":"0","visibility":"25.91"},"astronomy":{"sunrise":"4:55 am","sunset":"7:44 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Beijing, Beijing, CN at 08:00 PM CST","lat":"39.90601","long":"116.387909","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*","pubDate":"Mon, 10 Jul 2017 08:00 PM CST","condition":{"code":"31","date":"Mon, 10 Jul 2017 08:00 PM CST","temp":"32","text":"Clear"},"forecast":[{"code":"32","date":"10 Jul 2017","day":"Mon","high":"38","low":"22","text":"Sunny"},{"code":"30","date":"11 Jul 2017","day":"Tue","high":"35","low":"22","text":"Partly Cloudy"},{"code":"34","date":"12 Jul 2017","day":"Wed","high":"36","low":"26","text":"Mostly Sunny"},{"code":"4","date":"13 Jul 2017","day":"Thu","high":"37","low":"27","text":"Thunderstorms"},{"code":"4","date":"14 Jul 2017","day":"Fri","high":"35","low":"26","text":"Thunderstorms"},{"code":"4","date":"15 Jul 2017","day":"Sat","high":"31","low":"26","text":"Thunderstorms"},{"code":"4","date":"16 Jul 2017","day":"Sun","high":"32","low":"25","text":"Thunderstorms"},{"code":"30","date":"17 Jul 2017","day":"Mon","high":"33","low":"25","text":"Partly Cloudy"},{"code":"30","date":"18 Jul 2017","day":"Tue","high":"34","low":"25","text":"Partly Cloudy"},{"code":"30","date":"19 Jul 2017","day":"Wed","high":"35","low":"25","text":"Partly Cloudy"}],"description":"<![CDATA[<img src=\\"http://l.yimg.com/a/i/us/we/52/31.gif\\"/>\\n<BR />\\n<b>Current Conditions:</b>\\n<BR />Clear\\n<BR />\\n<BR />\\n<b>Forecast:</b>\\n<BR /> Mon - Sunny. High: 38Low: 22\\n<BR /> Tue - Partly Cloudy. High: 35Low: 22\\n<BR /> Wed - Mostly Sunny. High: 36Low: 26\\n<BR /> Thu - Thunderstorms. High: 37Low: 27\\n<BR /> Fri - Thunderstorms. High: 35Low: 26\\n<BR />\\n<BR />\\n<a href=\\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*\\">Full Forecast at Yahoo! Weather</a>\\n<BR />\\n<BR />\\n(provided by <a href=\\"http://www.weather.com\\" >The Weather Channel</a>)\\n<BR />\\n]]>","guid":{"isPermaLink":"false"}}}}}}'
登入後複製

  

以上是帶有 Oauth1 的雅虎天氣 API的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板