【问题标题】:PyOWM Daily Weather IconPyOWM 每日天气图标
【发布时间】:2021-03-23 17:58:59
【问题描述】:

我研究了 PyOWM 和 OWM API 指南以及一些代码配方,并且在大量代码中可以正常工作,但我就是不知道如何获取今天预报的当前天气图标?这是一个与图标不同的代码 sn-p:

from pyowm.owm import OWM
from pyowm.utils import timestamps
import requests, json

def checkInternetRequests(url='http://www.google.com/', timeout=3):
    try:
        r = requests.head(url, timeout=timeout)
        return True
    except requests.ConnectionError as ex:
        print(ex)
        return False

def get_weather():
  if checkInternetRequests() == True:
    # 52.5766° N, 1.5438° W
    print ("Internet Up")
    CITY = "Atherstone, GB"
    LON = 1.54
    LAT = 52.576 
    API_KEY = "myapikey"
    ICON_URL_1 = "http://openweathermap.org/img/wn/"
    ICON_URL_2 = "@2x.png"
    owm = OWM(API_KEY)
    mgr = owm.weather_manager()
    one_call = mgr.one_call(lat=LAT, lon=LON)

    print (one_call.forecast_hourly[3]) #works - 3hrs from now
    print ("===========================")
    print (one_call.forecast_daily[0]) # works - Today
    print ("===========================")
    print (one_call.forecast_daily[0].weather.icon)

  else:
    print ("Internet Down")
    time.sleep(10)

get_weather()

我收到以下错误:

Traceback (most recent call last):
  File "weather1.py", line 37, in <module>
    get_weather()
  File "weather1.py", line 31, in get_weather
    print (one_call.forecast_daily[0].weather.icon)
AttributeError: 'Weather' object has no attribute 'weather'

我哪里错了?谢谢!

【问题讨论】:

    标签: python-3.x openweathermap


    【解决方案1】:

    好的 - 喝了几杯啤酒和一些食物,再加上 4 小时的代码弄乱后,我可以正常工作了!

    one_call.forecast_daily[0].weather_icon_url()
    

    给了我一个网址,我可以用它在我的代码中显示今天的预测图标。

    【讨论】:

      【解决方案2】:
      import pyowm
      from pyowm.utils import timestamps, formatting
      from pyowm.owm import OWM
      import matplotlib.pyplot as plt
      import matplotlib.image as mpimg
      
      observation = mgr.weather_at_place('London')
      w = observation.weather
      icon = w.weather_icon_url(size='2x')
      
      img = mpimg.imread(icon)
      plt.imshow(img)
      plt.show()
      

      【讨论】:

      • 欢迎来到 StackOverflow。虽然此代码可能会解决问题,但包括解释如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请编辑您的答案以添加解释并说明适用的限制和假设。看看这里→How do I write a good answer?
      猜你喜欢
      • 2021-05-29
      • 2023-03-21
      • 1970-01-01
      • 2014-03-31
      • 2013-07-06
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      相关资源
      最近更新 更多