Dec-15-2023, 10:06 AM
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import requests from bs4 import BeautifulSoup import json import re def estrai_statistiche_partite(links_partite): dati_partite = [] for link_partita in links_partite: statistiche_partita = estrai_statistiche_partita(link_partita) if statistiche_partita is not None : dati_partite.append(statistiche_partita) for partita in dati_partite: team1_name = partita.get( 'Home Team' ) or partita.get( 'Team 1' ) team2_name = partita.get( 'Away Team' ) or partita.get( 'Team 2' ) if team1_name is not None and team2_name is not None : partita[ 'Partita' ] = f "{team1_name} - {team2_name}" return dati_partite def estrai_statistiche_partita(url): headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' } response = requests.get(url, headers = headers) if response.status_code ! = 200 : print ( f "Skipping invalid match (HTTP status code {response.status_code}): {url}" ) return None soup = BeautifulSoup(response.content, 'html.parser' ) team1_element = soup.find( 'div' , class_ = 'event-stats-section-header__team--home' ) team2_element = soup.find( 'div' , class_ = 'event-stats-section-header__team--away' ) if team1_element is None or team2_element is None : print ( f "Skipping invalid match: {url}" ) return None team1_name = team1_element.find( 'span' ).text.strip() if team1_element.find( 'span' ) else "Team 1 Name Not Found" team2_name = team2_element.find( 'span' ).text.strip() if team2_element.find( 'span' ) else "Team 2 Name Not Found" stat_items = soup.find_all( 'div' , class_ = 'event-stats-item' ) team1_stats = {} team2_stats = {} for item in stat_items: label = item[ 'label' ] home_value_str = item.find( 'div' , class_ = 'event-stats-item__home' ).get_text(strip = True ) away_value_str = item.find( 'div' , class_ = 'event-stats-item__away' ).get_text(strip = True ) print ( f "Label: {label}, Home Value: {home_value_str}, Away Value: {away_value_str}" ) # Rimuovi il simbolo '%' e converte la stringa in un numero home_value = float (home_value_str.rstrip( '%' )) away_value = float (away_value_str.rstrip( '%' )) # Utilizza copy.deepcopy per creare copie separate per ogni squadra team1_stats[label] = home_value team2_stats[label] = away_value scoring_minutes_items = soup.find_all( 'div' , class_ = 'event-stats-scoring-minutes__list__item' ) for item in scoring_minutes_items: label = item.find( 'span' , class_ = 'event-stats-scoring-minutes__list__label' ).get_text(strip = True ) team1_goals = int (re.search(r 'height:\s*(\d+)%' , item.find( 'div' , class_ = 'event-stats-progress-bar-vertical__lines__full' )[ 'style' ]).group( 1 )) team2_goals = int (re.search(r 'height:\s*(\d+)%' , item.select_one( 'div:nth-child(2) > div > div.event-stats-progress-bar-vertical__lines__full' )[ 'style' ]).group( 1 )) team1_stats[ f "{label} - Team 1" ] = team1_goals team2_stats[ f "{label} - Team 2" ] = team2_goals return { 'Team 1' : team1_name, 'Team 2' : team2_name, 'Team 1 Stats' : team1_stats, 'Team 2 Stats' : team2_stats } def salva_dati_su_file(dati_partite, nome_file): # Sovrascrivi completamente il file con i nuovi dati with open (nome_file, 'w' , encoding = 'utf-8' ) as file : json.dump(dati_partite, file , ensure_ascii = False , indent = 2 ) # Aprire il file JSON "matches_data.json" with open ( 'matches_pre.json' , 'r' , encoding = 'utf-8' ) as file : data = json.load( file ) # Estrai i link delle partite dal campo "Match Link" e chiama la funzione per estrarre le statistiche links_partite = [match[ 'Match Link' ] for match in data] dati_partite = estrai_statistiche_partite(links_partite) # Salva le statistiche delle partite su un nuovo file JSON salva_dati_su_file(dati_partite, 'stat_partitepre.json' ) |
in particular for fields "Over 0.5 goals" "Over 1.5 goals" "Over 2.5 goals" "Over 3.5 goals"
ie:
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
{ "Team 1" : "Nong Bua Lamphu" , "Team 2" : "Lampang FC" , "Team 1 Stats" : { "Position" : 1.0 , "Won" : 9.0 , "Drawn" : 4.0 , "Lost" : 2.0 , "Goals scored" : 2.1 , "Goals conceded" : 1.0 , "Over 0.5 goals" : 100.0 , "Over 1.5 goals" : 67.0 , "Over 2.5 goals" : 33.0 , "Over 3.5 goals" : 17.0 , "Over 4.5 goals" : 0.0 , "Over 1.5 Goals at Halftime" : 0.0 , "Scored in both halves percentage" : 0.0 , "Conceded in both halves percentage" : 0.0 , "Total %" : 33.0 , "Last 5 Matches" : 40.0 , "Home BTTS" : 33.0 , "Away BTTS" : 33.0 , "BTTS & Over 2.5 Goals" : 17.0 , "BTTS & Win" : 0.0 , "BTTS & LOST" : 0.0 , "BTTS in 1st Half" : 0.0 , "Won-to-nil" : 50.0 , "Lost-to-nil" : 17.0 , "Clean sheets" : 50.0 , "Over 9.5" : 17.0 , "Over 10.5" : 17.0 , "Over 11.5" : 0.0 , "Over 12.5" : 0.0 , "Over 13.5" : 0.0 , "Over 2.5" : 33.0 , "Over 3.5" : 17.0 , "Over 4.5" : 0.0 , "Over 5.5" : 0.0 , "Over 6.5" : 0.0 }, "Team 2 Stats" : { "Position" : 12.0 , "Won" : 5.0 , "Drawn" : 4.0 , "Lost" : 6.0 , "Goals scored" : 1.0 , "Goals conceded" : 1.2 , "Over 0.5 goals" : 100.0 , "Over 1.5 goals" : 67.0 , "Over 2.5 goals" : 33.0 , "Over 3.5 goals" : 17.0 , "Over 4.5 goals" : 0.0 , "Over 1.5 Goals at Halftime" : 0.0 , "Scored in both halves percentage" : 0.0 , "Conceded in both halves percentage" : 0.0 , "Total %" : 33.0 , "Last 5 Matches" : 40.0 , "Home BTTS" : 33.0 , "Away BTTS" : 33.0 , "BTTS & Over 2.5 Goals" : 17.0 , "BTTS & Win" : 0.0 , "BTTS & LOST" : 0.0 , "BTTS in 1st Half" : 0.0 , "Won-to-nil" : 17.0 , "Lost-to-nil" : 50.0 , "Clean sheets" : 17.0 , "Over 9.5" : 17.0 , "Over 10.5" : 17.0 , "Over 11.5" : 0.0 , "Over 12.5" : 0.0 , "Over 13.5" : 0.0 , "Over 2.5" : 33.0 , "Over 3.5" : 17.0 , "Over 4.5" : 0.0 , "Over 5.5" : 0.0 , "Over 6.5" : 0.0 }, "Partita" : "Nong Bua Lamphu - Lampang FC" }, |
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 |
[ { "Partita" : "Colchester United U21 - Watford U21" , "Team 1" : "Colchester United U21" , "Team 2" : "Watford U21" , }, { "Partita" : "CARA Brazzaville - AS JUK" , "Team 1" : "CARA Brazzaville" , "Team 2" : "AS JUK" , }, { "Partita" : "Recreativo do Libolo - Petro Luanda" , "Team 1" : "Recreativo do Libolo" , "Team 2" : "Petro Luanda" , }, { "Partita" : "Madiba FC - Abeokuta Stormers" , "Team 1" : "Madiba FC" , "Team 2" : "Abeokuta Stormers" , }, { "Partita" : "Saint Louis Suns - Bazar Brothers FC" , "Team 1" : "Saint Louis Suns" , "Team 2" : "Bazar Brothers FC" , } ] |