Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import bs4
- from yandex.Translater import Translater
- def translate(city): # возвращает перевод города с русского на английский с заменой пробела на тире
- tr = Translater()
- tr.set_key('key')
- tr.set_from_lang('ru')
- tr.set_to_lang('en')
- tr.set_text(city)
- return tr.translate().replace(" ", "-")
- def get_weather(city):
- city_trnsltd = translate(city)
- if city_trnsltd == "St.-Petersburg": #костыль
- city_trnsltd = city_trnsltd.replace("St.", "Saint")
- else:
- pass
- url = "https://yandex.ru/pogoda/{}".format(city_trnsltd)
- response = requests.get(url)
- soup = bs4.BeautifulSoup(response.content, "lxml")
- try:
- temp = soup.find("span", {"class": "temp__value"}).text + "°C"
- try:
- condition = soup.find("div", {"class": "link__condition"}).text
- except:
- condition = "Нет данных :("
- try:
- speed = soup.find_all("dd", {"class": "term__value"})[2].text
- except:
- speed = "Нет данных :("
- in_town = soup.find("div", {"class": "header-title__title-wrap"}).text
- try:
- humidity = soup.find_all("dd", {"class": "term__value"})[3].text
- except:
- humidity = "Нет данных :("
- try:
- pressure = soup.find_all("dd", {"class": "term__value"})[4].text
- except:
- pressure = "Нет данных :("
- return in_town + ": " + "\n" + "\n" + " Температура: " + temp + "\n" + " Состояние: " + condition + "\n" + " Скорость ветра: " + speed + "\n" + " Влажность: " + humidity + "\n" + " Атмосферное давление: " + pressure
- except:
- return "Город не найден :("
Add Comment
Please, Sign In to add comment