Home  >  Article  >  Backend Development  >  Yahoo Weather API with Oauth1

Yahoo Weather API with Oauth1

PHP中文网
PHP中文网Original
2017-07-11 18:13:231726browse

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"}}}}}}'

  

The above is the detailed content of Yahoo Weather API with Oauth1. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn