import html
from urllib.request import urlopen, Request
ACCU_URL = 'https://www.accuweather.com/uk/ua/lviv/324561/weather-forecast/324561'
ACCU_TAGS = ('<span class="large-temp">', '<span class="cond">')
def get_request_headers():
return {'User-Agent': 'Mozilla/5.0(X11; Ubuntu; Linux x86_64;)'}
def get_page_source(url):
request = urllib.request.Request(url, headers=get_request_headers())
page_source = urllib.request.urlopen(request).read()
return page_source.decode('utf-8')
def get_tag_content(page_content, tag):
tag_index = page_content.find(tag)
tag_size = len(tag)
value_start = tag_index + tag_size
content = ''
for c in page_content[value_start:]: # type: object
if c != '<':
content += c
else:
break
return content
def get_weather_info(page_content, tags):
return tuple([get_tag_content(page_content, tag) for tag in tags])
def produce_output(provider_name, temp, condition):
print('{provider_name}: \n')
print('{html.unescape(temp)} \n')
print('{(condition)} \n')
def main():
weather_sites = {"AccuWeather": (ACCU_URL, ACCU_TAGS)}
for name in weather_sites:
url, tags = weather_sites[name]
content = get_page_source(url)
temp, condition = get_weather_info(content, tags)
produce_output(name, temp, condition)
if __name__ == 'main':
main()