如何修改Python代码以仅抓取LiveScore网站的实时足球比赛数据?
Solution: Filter Live Matches & Fetch Current Match Minute
Let's fix your code to only capture in-progress matches and pull the live minute count instead of kickoff time. Here's how to adjust it step by step:
import requests import pandas as pd url = "https://prod-public-api.livescore.com/v1/api/react/date/soccer/20220309/0.00?MD=1" jsonData = requests.get(url).json() rows = [] for stage in jsonData['Stages']: events = stage['Events'] for event in events: # Only keep matches that are currently live (status code 2 = in progress) if event.get('St') == 2: home_team = event['T1'][0]['Nm'] away_team = event['T2'][0]['Nm'] # Grab live minute (supports stoppage time like "90+3") current_minute = event.get('Min', 'N/A') row = { 'Home Team': home_team, 'Away Team': away_team, 'Current Minute': current_minute } rows.append(row) # Build the final DataFrame with live match data live_matches_df = pd.DataFrame(rows) print(live_matches_df)
Key Details:
- Live Match Filter: The
Stfield in each event object tells us the match state. A value of2means the game is actively in progress (other common codes:0= not started,3= finished). - Current Minute Handling: The
Minfield directly returns the live time, including stoppage time in formats like45+2or90+5. We useevent.get()to fall back toN/Ajust in case the field is missing (rare for live games). - Removed Kickoff Time: Since we're only targeting live matches, we don't need to parse the
Esdkickoff timestamp anymore.
Quick Tip:
To dynamically fetch today's live matches instead of hardcoding the date (20220309), generate the current date in YYYYMMDD format with:
from datetime import datetime today_date = datetime.today().strftime('%Y%m%d') url = f"https://prod-public-api.livescore.com/v1/api/react/date/soccer/{today_date}/0.00?MD=1"
内容的提问来源于stack exchange,提问作者Carolino
相关产品推荐
相关产品推荐

