@ElPerinson

Добрый ночи, пытаюсь запустить погодного бота для телеграм, выдает ошибки, в чем проблема?

C:\Users\user\PycharmProjects\pythonProject\main.py:23: DeprecationWarning: There is no current event loop
loop = asyncio.get_event_loop()
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject\main.py", line 24, in
loop.run_until_complete(getweather())
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
return future.result()
File "C:\Users\user\PycharmProjects\pythonProject\main.py", line 9, in getweather
weather = await client.find("New York")
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\python_weather\client.py", line 36, in find
return await self.http.request(location)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\python_weather\rest.py", line 19, in request
parsed = parse(await resp.text())
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\xmltodict.py", line 378, in parse
parser.Parse(xml_input, True)
xml.parsers.expat.ExpatError: mismatched tag: line 4, column 2
Unclosed client session
client_session:
Unclosed connector
connections: ['[(, 1349.796)]']
connector:

Process finished with exit code 1
___________________________________________________________________________________________________________

код

import python_weather
import asyncio

async def getweather():
# declare the client. format defaults to the metric system (celcius, km/h, etc.)
client = python_weather.Client(format=python_weather.IMPERIAL)

weather = await client.find("New York")

# returns the current day's forecast temperature (int)
print(weather.current.temperature)

# get the weather forecast for a few days
for forecast in weather.forecasts:
print(str(forecast.date), forecast.sky_text, forecast.temperature)

# close the wrapper once done
await client.close()

if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(getweather())

Работаю на pychram 2022.1.1, python 3.11.1
  • Вопрос задан
  • 494 просмотра
Пригласить эксперта
Ответы на вопрос 1
@igor6130
Прежде всего вы запускаете асинхронный код устаревшим способом. Вместо
loop = asyncio.get_event_loop()
loop.run_until_complete(getweather())

нужно делать так
asyncio.run(getweather())

И в целом проблема в модуле python_weather. Там возникает ошибка парсинга полученных данных.

Плюс client = python_weather.Client(format=python_weather.IMPERIAL) скорее всего доже работать как контекстный менеджер, учитывая наличие await client.close().
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
18 мая 2024, в 21:07
5000 руб./за проект
18 мая 2024, в 20:58
5000 руб./за проект
18 мая 2024, в 20:53
500 руб./за проект