-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixture_results.py
65 lines (53 loc) · 1.62 KB
/
fixture_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import csv
from datetime import datetime
from bs4 import BeautifulSoup
standings_url ="https://fbref.com/en/comps/9/2020-2021/schedule/2020-2021-Premier-League-Scores-and-Fixtures"
data = requests.get(standings_url)
soup = BeautifulSoup(data.text)
standings = soup.select('table.stats_table')[0]
main_list=[]
for team in standings.find_all('tbody'):
rows = team.find_all('tr')
for i in rows:
# round and day
round = i.find('th',{'data-stat':'gameweek'})
if round!=None:
Round = round.text
day = i.find('td',{'data-stat':'dayofweek'})
if day!= None:
Day=day.text
#date
date=i.find('td',{'data-stat':'date'})
date = date.find('a')
if date!=None:
Date=date.text
#converting date formats
date_obj = datetime.strptime(Date, "%Y-%m-%d")
Date = date_obj.strftime("%b %d %Y")
#home_team
ht= i.find('td',{'data-stat':'home_team'})
ht=ht.find('a')
if ht !=None:
Ht=ht.text
#score
sc= i.find('td',{'data-stat':'score'})
sc=sc.find('a')
if sc!=None:
Sc=sc.text
#away_team
at= i.find('td',{'data-stat':'away_team'})
at=at.find('a')
if at!=None:
At=at.text
#each game
if Round =='':
continue
game = Round + ',' + Day + ' ' + Date + ',' +Ht + ',' + Sc + ',' + At
main_list.append(game)
main_list.sort(key = lambda x: int(x.split(',')[0]))
with open('fixture_results.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Round", "Date", "Team 1", "FT", "Team 2"])
for row in main_list:
writer.writerow(row.split(','))