Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
oddspedia charts
#21
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')
i've a problem for some fields because in output i have same value for home and away also if they are different in source of webpage.
in particular for fields "Over 0.5 goals" "Over 1.5 goals" "Over 2.5 goals" "Over 3.5 goals"

ie:
{
    "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"
  },
my matches.json has a structure like this:

 [ {
    "Partita": "Colchester United U21 - Watford U21",
    "Team 1": "Colchester United U21",
    "Team 2": "Watford U21",
    "Additional Image URL": "https://cdn.oddspedia.com/images/teams/medium/1/18080.png",
    "Match Link": "https://oddspedia.com/football/watford-u21-colchester-united-u21-1006595"
  },
  {
    "Partita": "CARA Brazzaville - AS JUK",
    "Team 1": "CARA Brazzaville",
    "Team 2": "AS JUK",
    "Additional Image URL": "https://cdn.oddspedia.com/images/teams/medium/1/18080.png",
    "Match Link": "https://oddspedia.com/football/cara-brazzaville-as-juk-1256630"
  },
  {
    "Partita": "Recreativo do Libolo - Petro Luanda",
    "Team 1": "Recreativo do Libolo",
    "Team 2": "Petro Luanda",
    "Additional Image URL": "https://cdn.oddspedia.com/images/teams/medium/1/18080.png",
    "Match Link": "https://oddspedia.com/football/recreativo-do-libolo-petro-luanda-228781"
  },
  {
    "Partita": "Madiba FC - Abeokuta Stormers",
    "Team 1": "Madiba FC",
    "Team 2": "Abeokuta Stormers",
    "Additional Image URL": "https://cdn.oddspedia.com/images/teams/medium/1/18080.png",
    "Match Link": "https://oddspedia.com/football/abeokuta-stormers-madiba-fc-1257254"
  },
  {
    "Partita": "Saint Louis Suns - Bazar Brothers FC",
    "Team 1": "Saint Louis Suns",
    "Team 2": "Bazar Brothers FC",
    "Additional Image URL": "https://cdn.oddspedia.com/images/teams/medium/1/18080.png",
    "Match Link": "https://oddspedia.com/football/saint-louis-suns-bazar-brothers-fc-1107034"
  }
]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Configuration file through helm charts saisankalpj 0 1,734 Aug-23-2022, 01:32 PM
Last Post: saisankalpj
  Cannot Extract data through charts online AgileAVS 0 1,852 Feb-01-2020, 01:47 PM
Last Post: AgileAVS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020