from bs4 import BeautifulSoup from dateutil.parser import parse import json year = 1952 with open("data/"+str(year)+".html") as fp: soup = BeautifulSoup(fp, 'html.parser') start_pos = soup.find(string="1952 Regular Season - Week 1"); obj = { "games":[] } for table in start_pos.find_all_next(class_="soh1"): for child in table.find_all("tbody"): for row in child.find_all("tr"): cells = row.find_all("td") game = {} cursor = 0 if parse(cells[1].string).year!=year: cursor = cursor+1 game["date"] = cells[cursor+1].string game["at"] = cells[cursor+3].string game["fav"] = cells[cursor+4].string game["score"] = cells[cursor+5].string obj["games"].append(game) objson = json.dumps(obj) print(objson)