Created
February 13, 2017 14:22
-
-
Save Gijs-Koot/ee2bd8b2da0e56c7ab13fc25d8ef4291 to your computer and use it in GitHub Desktop.
Download and parse current year KNVB eredivisie results and schedule from their site.
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
from bs4 import BeautifulSoup | |
import requests | |
import dateparser | |
import pandas as pd | |
uitslagen_url = "http://www.knvb.nl/competities/eredivisie/uitslagen" | |
programma_url = "http://www.knvb.nl/competities/eredivisie/programma" | |
uitslagen_soup = BeautifulSoup(requests.get(uitslagen_url).content, "lxml") | |
programma_soup = BeautifulSoup(requests.get(programma_url).content, "lxml") | |
def parse_timetable_page(soup, parse_score=True): | |
records = list() | |
for table in soup.select(".table-timetable"): | |
date = dateparser.parse(table.select_one(".header span").text) | |
for row in table.select("div.row"): | |
try: | |
home = row.select_one(".home > .team").text | |
away = row.select_one(".away > .team").text | |
r = { | |
"date": date, | |
"home": home, | |
"away": away | |
} | |
if parse_score: | |
home_score, away_score = row.select_one(".value.center").text.split("-") | |
r.update({ | |
"home_score": int(home_score), | |
"away_score": int(away_score) | |
}) | |
records.append(r) | |
except Exception as e: | |
print(e) | |
return records | |
uitslagen = parse_timetable_page(uitslagen_soup) | |
programma = parse_timetable_page(programma_soup, parse_score=False) | |
ed = pd.DataFrame(uitslagen + programma) | |
ed.to_csv("./games.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment