TypeError:weather_at_place()接受2个位置参数,但给出了4个

2024-09-28 03:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我导入了一个名为“pyown”的库来使用python进行本地化,我还安装了另一个库,它调用请求来获取气象数据。在

我的剧本:


import pyown
import requests

res = requests.get('https://ipinfo.io/')
data = res.json()
Cidade = str(data['city'])
Pais = str(data['country'])
Pais2 = Pais.lower()

observation = owm.weather_at_place(Cidade ,   ','   , Pais2)
w = observation.get_weather()
temperature = w.get_temperature('celsius')
wind = w.get_wind()

我有个错误:

^{pr2}$

有人能帮我吗?在


Tags: importdatagetres气象requestswindweather
3条回答

试着只给它2个参数。在

观察=哦,天气预报(Cidade,Pais2)

您可以从代码中检查weather_at_place方法: https://github.com/csparpa/pyowm/blob/master/pyowm/weatherapi25/owm25.py#L210

 def weather_at_place(self, name):
     """
     Queries the OWM Weather API for the currently observed weather at the
     specified toponym (eg: "London,uk")
     :param name: the location's toponym
     :type name: str or unicode
     :returns: an *Observation* instance or ``None`` if no weather data is available
     :raises: *ParseResponseException* when OWM Weather API responses' data cannot be parsed or *APICallException* when OWM Weather API can not be reached
     """

您可以看到它只接受name作为单个字符串。在

您也可以查看并遵循文档中的示例:
https://pyowm.readthedocs.io/en/latest/usage-examples-v2/weather-api-object-model.html#the-owm25-class

CURRENT WEATHER QUERYING
* find current weather at a specific location -> eg: owm.weather_at_place('London,UK')

从文档(https://buildmedia.readthedocs.org/media/pdf/pyowm/latest/pyowm.pdf)来看,您希望将城市和国家作为一个字符串。尝试类似于:

observation = owm.weather_at_place(Cidade + ',' + Pais2)

或者

observation = owm.weather_at_place('{},{}'.format(Cidade, Pais2))

相关问题 更多 >

    热门问题