Created
July 29, 2023 19:34
-
-
Save ChimekKoo/b067bdb0cac1465f15c81803108e1572 to your computer and use it in GitHub Desktop.
Download lift times of the Tower Bridge in London from it's official 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
import requests, bs4 | |
def get_lift_times(from_date: "str|None" = None, to_date: "str|None" = None, page: int = 0) -> "list[dict]": | |
"""Downloads the Tower Bridge in London lift times from it's official website. | |
Args: | |
from_date: getting lift times from that date (inclusive) | |
to_date: getting lift times to that date (inclusive) | |
page: number of lift times page indexed from zero | |
Yields: | |
Lift time records as dicts in format: | |
`{"datetime": "YYYY-MM-DD HH:mm:SS", "vessel": "<vessel-name>", "direction": "<up/down>"}` | |
(date/time in the timezone of the Tower Bridge in London) | |
""" | |
invert_date = lambda s: "-".join(reversed(s.split("-"))) | |
if from_date is not None: from_date = invert_date(from_date) | |
if from_date is not None: to_date = invert_date(to_date) | |
resp = requests.get("https://www.towerbridge.org.uk/lift-times", params={ | |
"lifts_times_from": from_date, | |
"lifts_times_to": to_date, | |
"page": page, | |
}) | |
resp.raise_for_status() | |
table = bs4.BeautifulSoup(resp.text, 'html.parser').find("div", class_="view-bridge-opening-times").find("tbody") | |
if table is None: | |
yield from () | |
return | |
yield from ( | |
{ | |
"datetime": row.find("td", class_="views-field-field-date-time-1").find("time")["datetime"][:-1].replace("T"," "), | |
"vessel": row.find("td", class_="views-field-field-vessel").text.strip(), | |
"direction": row.find("td", class_="views-field-field-direction").text.strip().lower().split()[0], | |
} | |
for row in table.find_all("tr") | |
) | |
if __name__ == "__main__": | |
res = [] | |
page = 0 | |
while True: | |
bef = len(res) | |
res += get_lift_times(page=page) | |
if len(res)-bef == 0: | |
break | |
page += 1 | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment