>백엔드 개발 >파이썬 튜토리얼 >Oauth1을 사용하는 야후 날씨 API

Oauth1을 사용하는 야후 날씨 API

PHP中文网
PHP中文网원래의
2017-07-11 18:13:231782검색

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.