viking_unet

Untitled

Jun 11th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup as BS
  3.  
  4. def get_weather(city):
  5.  
  6.     r = requests.get('https://sinoptik.ua/погода-%s' % city.lower())
  7.     #r = requests.get('https://sinoptik.com.by/погода-%s' % city.lower())
  8.     html = BS(r.content, 'html.parser')
  9.     res_ua = ''
  10.     try:
  11.         t_min  = html.select('.temperature .min')[0].text
  12.         t_max  = html.select('.temperature .max')[0].text
  13.         descr  = html.select('.wDescription .description')[0].text
  14.         res_ua = '%s %s %s' % (t_min, t_max, descr.strip())
  15.     except Exception as err:
  16.         print('error get weather from .ua site')
  17.         pass
  18.     res_by = ''
  19.     try:
  20.         temp  = ' '.join(html.find('div', class_ = 'weather__content_tab current').
  21.                          find('div', class_ = 'weather__content_tab-temperature').
  22.                          text.split())
  23.         descr  = html.find('div', class_ = 'weather__article_description-text').text
  24.         res_by = '%s %s' % (temp, descr)
  25.     except Exception as err:
  26.         print('error get weather from .com.by site')
  27.         pass
  28.     res = res_ua or res_by
  29.     if not res:
  30.         return ('не найдена погода для города "%s"' % city)
  31.     else:
  32.         return res
  33.    
  34. res = get_weather('Гродно')
  35. print(res)
Add Comment
Please, Sign In to add comment