Created
January 11, 2021 22:54
-
-
Save zeevox/802a2697a73d037cc303b21c3fe3b7ef to your computer and use it in GitHub Desktop.
Fetch UK weather extremes from the Met Office website
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
#!/usr/bin/python3 | |
import requests | |
from bs4 import BeautifulSoup | |
import argparse | |
parser = argparse.ArgumentParser(description='Fetch UK weather extremes from the Met Office website') | |
parser.add_argument('--print-headers', dest='print_headers', action='store_true', help='Print CSV column headers') | |
args = parser.parse_args() | |
page = requests.get("https://www.metoffice.gov.uk/public/weather/observation") | |
soup = BeautifulSoup(page.content, 'html.parser') | |
data = {} | |
table = soup.select_one(".table.extremes") | |
for row in table.select_one("tbody").select("tr"): | |
parsed = [entry.text.strip() for entry in row.select("td")] | |
data[f"{parsed[0]} - Location"] = parsed[1] | |
value = parsed[2].split("\xa0") | |
data[f"{parsed[0]} - Value ({value[1]})"] = value[0] | |
if args.print_headers: | |
print(",".join(data.keys())) | |
print(",".join(data.values())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment