Skip to content

Instantly share code, notes, and snippets.

@ZeevoX
Created January 11, 2021 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZeevoX/802a2697a73d037cc303b21c3fe3b7ef to your computer and use it in GitHub Desktop.
Save ZeevoX/802a2697a73d037cc303b21c3fe3b7ef to your computer and use it in GitHub Desktop.
Fetch UK weather extremes from the Met Office website
#!/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