首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Weather API、Python或JavaScript

Weather API、Python或JavaScript
EN

Stack Overflow用户
提问于 2013-04-21 13:25:25
回答 2查看 3.6K关注 0票数 0

我正在尝试让天气API工作。但是我怎么才能只得到C°和天气图标,而不是别的呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-21 13:42:09

在使用pip安装了pywapi之后,打开一个ipython shell并探索pywapi可以做什么。这里有一个例子,它实际上给了你你想要的:

代码语言:javascript
复制
In [1]: import pywapi

In [2]: pywapi.
pywapi.GOOGLE_CITIES_URL                   pywapi.YAHOO_WEATHER_URL                   pywapi.get_weather_from_weather_com        pywapi.urlopen
pywapi.GOOGLE_COUNTRIES_URL                pywapi.getText                             pywapi.get_weather_from_yahoo              pywapi.wind_beaufort_scale
pywapi.NOAA_WEATHER_URL                    pywapi.get_cities_from_google              pywapi.minidom                             pywapi.wind_direction
pywapi.URLError                            pywapi.get_countries_from_google           pywapi.quote                               pywapi.xml_get_attrs
pywapi.WEATHER_COM_URL                     pywapi.get_everything_from_yahoo           pywapi.re                                  pywapi.xml_get_ns_yahoo_tag
pywapi.YAHOO_WEATHER_NS                    pywapi.get_weather_from_noaa               pywapi.sys                                 pywapi.yield_all_country_city_codes_yahoo

In [2]: pywapi.get_weather_from_yahoo?
Type:       function
String Form:<function get_weather_from_yahoo at 0x10daeb9b0>
File:       /Users/calvin/.virtualenvs/myweather/lib/python2.7/site-packages/pywapi.py
Definition: pywapi.get_weather_from_yahoo(location_id, units='metric')
Docstring:
Fetches weather report from Yahoo! Weather

Parameters:
  location_id: A five digit US zip code or location ID. To find your location ID,
  browse or search for your city from the Yahoo! Weather home page (http://weather.yahoo.com/)
  The weather ID is in the URL for the forecast page for that city. You can also get
  the location ID by entering your zip code on the home page. For example, if you
  search for Los Angeles on the Weather home page, the forecast page for that city
  is http://weather.yahoo.com/forecast/USCA0638.html. The location ID is USCA0638.

  units: type of units. 'metric' for metric and '' for non-metric
  Note that choosing metric units changes all the weather units to metric,
  for example, wind speed will be reported as kilometers per hour and
  barometric pressure as millibars.

Returns:
  weather_data: a dictionary of weather data that exists in XML feed.
  See http://developer.yahoo.com/weather/#channel

In [3]: pywapi.get_weather_from_yahoo('USCA0638')
Out[3]:
{'astronomy': {'sunrise': u'6:16 am', 'sunset': u'7:28 pm'},
 'atmosphere': {'humidity': u'67',
  'pressure': u'1013.7',
  'rising': u'1',
  'visibility': u'16.09'},
 'condition': {'code': u'33',
  'date': u'Sat, 20 Apr 2013 9:46 pm PDT',
  'temp': u'18',
  'text': u'Fair',
  'title': u'Conditions for Los Angeles, CA at 9:46 pm PDT'},
 'forecasts': [{'code': u'31',
   'date': u'20 Apr 2013',
   'day': u'Sat',
   'high': u'26',
   'low': u'15',
   'text': u'Clear'},
  {'code': u'32',
   'date': u'21 Apr 2013',
   'day': u'Sun',
   'high': u'26',
   'low': u'14',
   'text': u'Sunny'},
  {'code': u'34',
   'date': u'22 Apr 2013',
   'day': u'Mon',
   'high': u'22',
   'low': u'13',
   'text': u'Mostly Sunny'},
  {'code': u'30',
   'date': u'23 Apr 2013',
   'day': u'Tue',
   'high': u'21',
   'low': u'14',
   'text': u'Partly Cloudy'},
  {'code': u'30',
   'date': u'24 Apr 2013',
   'day': u'Wed',
   'high': u'19',
   'low': u'13',
   'text': u'Partly Cloudy'}],
 'geo': {'lat': u'34.05', 'long': u'-118.23'},
 'html_description': u'\n[![][5]][5]<br />\n<b>Current Conditions:</b><br />\nFair, 18 C<BR />\n<BR /><b>Forecast:</b><BR />\nSat - Clear. High: 26 Low: 15<br />\nSun - Sunny. High: 26 Low: 14<br />\nMon - Mostly Sunny. High: 22 Low: 13<br />\nTue - Partly Cloudy. High: 21 Low: 14<br />\nWed - Partly Cloudy. High: 19 Low: 13<br />\n<br />\n<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>\n',
 'link': u'http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_c.html',
 'location': {'city': u'Los Angeles', 'country': u'US', 'region': u'CA'},
 'title': u'Yahoo! Weather - Los Angeles, CA',
 'units': {'distance': u'km',
  'pressure': u'mb',
  'speed': u'km/h',
  'temperature': u'C'},
 'wind': {'chill': u'18', 'direction': u'0', 'speed': u'0'}}

正如您在结果中看到的,您可以在html_description (http://l.yimg.com/a/i/us/we/52/33.gif)和temp中获得天气图标。由于返回给您的结果就是一切,因此您需要提取/访问它,就像使用python字典一样。

更新

  1. 转到http://weather.yahoo.com/并键入您的邮政编码(或您所在的城市)。
  2. 如果有多个选项,请选择所需的一个。
  3. 在"6-10天“列中找到"extended forecast”链接。点击它。
  4. 查看网址地址栏,那里的数字就是你所在城市的邮政编码。

所有城市代码

下面的函数可以帮助您获取所有国家和城市的代码

代码语言:javascript
复制
In [4]: pywapi.yield_all_country_city_codes_yahoo?
Type:       function
String Form:<function yield_all_country_city_codes_yahoo at 0x10daebaa0>
File:       /Users/calvin/.virtualenvs/myweather/lib/python2.7/site-packages/pywapi.py
Definition: pywapi.yield_all_country_city_codes_yahoo(country_code, cities)
Docstring:
Yield all cities codes for a specific country.

Parameters:
  country_code: A four letter code of the necessary country. For example 'GMXX' or 'FRXX'.
  cities: The number of cities to yield

Returns:
  country_city_codes: A generator containing the city codes
票数 2
EN

Stack Overflow用户

发布于 2013-04-22 04:56:26

您可以使用Metwit weather api

如果您可以在客户端实现它们: 200个请求/天(基于ip的节流),则不需要身份验证。覆盖全球,兼容JSON和REST。你可以免费注册额外的API调用,如果你仍然需要调用它们的服务器端,基本的计划是相当便宜的。

下面是jQuery中的一个小示例:使用我们的weather resourcehttp://jsbin.com/isukam/1

完全公开:我拥有这个甜蜜的API。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16128232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档