Created
March 10, 2019 18:33
-
-
Save RMuskovets/c593aa67a77d9681475a4ef5b10ad72d to your computer and use it in GitHub Desktop.
Get weather data from the Sinoptik site!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup as Parser | |
FILE = open('WEATHER', 'w') # Our output file | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:45.0) Gecko/20100101 Firefox/45.0' # Mozilla Firefox (to avoid blocking) | |
} | |
def get_data(date): | |
url = 'https://ua.sinoptik.ua/%D0%BF%D0%BE%D0%B3%D0%BE%D0%B4%D0%B0-%D0%B7%D0%BE%D1%80%D1%8F-303008911/{}'.format(date) # URL for Zorya, Rivnens'ka Oblast' | |
r = requests.get(url, headers=headers) | |
soup = Parser(r.text, 'html.parser'); assert soup is not None | |
wrap = soup.find("div", id="wrapper"); assert wrap is not None | |
cont = wrap.find("div", id="content"); assert cont is not None | |
lcol = cont.find("div", id="leftCol"); assert lcol is not None | |
main = lcol.find("div", id="mainContentBlock"); assert main is not None | |
days = main.find("div", id="blockDays"); assert days is not None | |
tabs = days.find("div", class_="tabsContent"); assert tabs is not None | |
tbsi = tabs.find("div", class_="tabsContentInner"); assert tbsi is not None | |
bd1c = tabs.find("div", id="bd1c"); assert bd1c is not None | |
wmca = bd1c.find("div", class_="wMain clearfix archive"); assert wmca is not None | |
rsid = wmca.find("div", class_="rSide"); assert rsid is not None | |
wthr = rsid.find("table", class_="weatherDetails"); assert wthr is not None | |
body = wthr.find("tbody"); assert body is not None # Body of the table with OUR data | |
# Finding the weather icon | |
icos = body.find("tr", class_="weatherIcoS"); | |
icon = icos.find("td", class_="p3").find("div", class_="weatherIco")['title']; # Tooltip for weather icon | |
# Founding temperature in Celsius | |
tmpl = body.find("tr", class_="temperature"); # Temperature in Celsius degrees | |
temp = tmpl.find("td", class_="p3").string; | |
# Finding the wind | |
winds = body.find_all("tr")[5] | |
wind_ = winds.find("td", class_="p3") | |
wind = wind_.find('div')['data-tooltip'] # Wind | |
print('{}: {} *C, weather: {}, wind: {}'.format(date, temp, icon, wind), file=FILE) | |
FILE.flush() | |
dates = list((('2018-12-' + str(x) if len(str(x))==2 else '2018-12-0' + str(x) for x in range(31)))) + \ # December 2018 | |
list((('2019-01-' + str(x) if len(str(x))==2 else '2019-01-0' + str(x) for x in range(31)))) + \ # January 2019 | |
list((('2019-02-' + str(x) if len(str(x))==2 else '2019-02-0' + str(x) for x in range(11)))) # February 2019 | |
for date in dates: | |
get_data(date) | |
FILE.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment