CCCPaste

bexley_collection_scrape

from requests_html import HTMLSession
import json
from datetime import datetime
import calendar

now = datetime.now()
current_day = int(now.strftime("%d"))
current_month_nr = int(now.strftime("%m"))
current_month_name = calendar.month_name[current_month_nr]
current_year = int(now.strftime("%Y"))

session = HTMLSession()

url = "https://www.bexley.gov.uk/services/rubbish-and-recycling/bin-collection-calendar/rotation-week-1"

r = session.get(url)

# Get all tables with calendar
months = r.html.find("table.calendar")

# Iterate over every table
calendar = {}
for month in months:
    period = month.find('caption')[0].text
    month_dict = {}

    for i in month.find('span'):    
        month_dict.update( {int(i.attrs['title'].split(':')[0].split(" ")[1]): i.attrs['title'].split(':')[1].strip()} )
        
    calendar.update( {period:month_dict} )

# Serializing json
calendar_json = json.dumps(calendar, indent=4)

# Writing to bin_collection.json
with open("bin_collection.json", "w") as outfile:
    outfile.write(calendar_json)

# Example: Print output for January 2022, 15
print(f"\n {current_month_name} {current_year}: \n", f"Day {current_day}: ",calendar[f"{current_month_name} {current_year}"][current_day].title(), "\n")