-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
29 lines (20 loc) · 1011 Bytes
/
stats.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
import requests
import csv
from bs4 import BeautifulSoup
season = '2020-21'
URL = 'https://www.gazzetta.it/calcio/fantanews/statistiche/serie-a-' + season
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
fields = ['squadra', 'giocatore', 'ruolo', 'q', 'pg', 'g', 'a', 'mm', 'es', 'rt', 'r', 'rs', 'rp', 'mv', 'mm', 'mp']
rows = soup.find('table', class_='playerStats').find('tbody').find_all('tr')
with open(f"./output_{season}.csv", 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fields)
writer.writeheader()
for row in rows:
player_data = {
'squadra': row.find('td', 'field-sqd').find('span', 'hidden-team-name').text,
'giocatore': row.find('td', 'field-giocatore').find('a').text
}
for column in ['ruolo', 'q', 'pg', 'g', 'a', 'mm', 'es', 'rt', 'r', 'rs', 'rp', 'mv', 'mm', 'mp']:
player_data[column] = row.find('td', 'field-' + column).text.strip()
writer.writerow(player_data)