Created
June 13, 2018 04:39
-
-
Save CavaTrendy/62c121ac68f4eaa36c6664f8ab416894 to your computer and use it in GitHub Desktop.
Weather for the Netherlands
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 | |
import bs4 | |
import collections | |
''' | |
Created with the help of Python Jumpstart by Building 10 Apps | |
''' | |
WeatherReport = collections.namedtuple('WeatherReport', | |
'cond, temp, scale, loc') | |
'''' | |
This is the first part where we define teh main function of the program Weather App | |
''' | |
def main(): | |
print_the_header() | |
code_zip = input('What city for the weather in the Netherlands(wageningen)? ') | |
html = get_html_from_web(code_zip) | |
report = get_weather_from_html(html) | |
print('The temp in {} is {} {} and {}'.format( | |
report.loc, | |
report.temp, | |
report.scale, | |
report.cond | |
)) | |
def print_the_header(): | |
print('---------------------') | |
print(' WEATHER APP') | |
print('---------------------') | |
print() | |
def get_html_from_web(place): | |
url = 'https://www.wunderground.com/weather/nl/{}'.format(place) | |
response = requests.get(url) | |
return response.text | |
def get_weather_from_html(html): | |
# cityCss = '.region-content-header h1' | |
# weatherScaleCss = '.wu-unit-temperature .wu-label' | |
# weatherTempCss = '.wu-unit-temperature .wu-value' | |
# weatherConditionCss = '.condition-icon' | |
soup = bs4.BeautifulSoup(html, 'html.parser') | |
loc = soup.find(class_='region-content-header').find('h1').get_text() | |
condition = soup.find(class_='condition-icon').get_text() | |
temp = soup.find(class_='wu-unit-temperature').find(class_='wu-value').get_text() | |
scale = soup.find(class_='wu-unit-temperature').find(class_='wu-label').get_text() | |
loc = cleanup_text(loc) | |
loc = find_city_and_state_from_location(loc) | |
condition = cleanup_text(condition) | |
temp = cleanup_text(temp) | |
scale = cleanup_text(scale) | |
report = WeatherReport(cond=condition, temp=temp, scale=scale, loc=loc) | |
return report | |
def find_city_and_state_from_location (loc: str): | |
parts = loc.split('\n') | |
return parts[0].strip() | |
def cleanup_text(text : str): | |
if not text: | |
return text | |
text = text.strip() | |
return text | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment