To extract the required match data from the provided vlr.gg match listing and compile it into a clean DataFrame with the specified columns, we'll use the requests and BeautifulSoup libraries for web scraping, and pandas for organizing the data.
✅ Overview of the Script
This script performs the following tasks:
-
Fetches the main match listing page to extract:
- Match URLs
- Match IDs
- Match Dates
- Team Names
- Series Scores (to determine the match winner)
-
Visits each individual match page to extract:
- Map names (e.g., Ascent, Bind)
- Map scores (e.g., 13-10)
-
Organizes the data into a DataFrame with the following columns:
match_id
date
team1
team2
map1, map1_score
map2, map2_score
map3, map3_score
match_winner
-
Saves the data to a CSV file (vlr_matches.csv) for further analysis.
🧠 Key Assumptions
- The match listing page contains exactly 30 matches, and the script processes up to 30 matches.
- Each match page has a maximum of 3 maps.
- Map data is found in
div.match-game elements on the individual match pages.
- Series results are parsed from the main page using the score elements.
🧾 Python Code
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
def get_match_data(match_url):
"""Scrape map details (name and score) from the individual match page."""
response = requests.get(match_url)
soup = BeautifulSoup(response.content, 'html.parser')
map_games = soup.find_all('div', class_='match-game')
maps_info = []
for game in map_games:
# Extract map name (e.g., "Game 1: Ascent")
header = game.find('div', class_='match-game-header-title')
if not header:
continue
map_name = header.text.strip()
if ':' in map_name:
map_name = map_name.split(':', 1)[1].strip()
# Extract scores
score1 = game.find('div', class_='match-game-scoreboard-team1')
score2 = game.find('div', class_='match-game-scoreboard-team2')
score_str = f"{score1.text.strip()}-{score2.text.strip()}" if score1 and score2 else None
maps_info.append({'name': map_name, 'score': score_str})
return maps_info
def main():
main_url = 'https://www.vlr.gg/event/matches/2347/champions-tour-2025-americas-stage-1/?series_id=4541'
response = requests.get(main_url)
soup = BeautifulSoup(response.content, 'html.parser')
# Find all match items (anchor tags with class 'match-item')
match_elements = soup.find_all('a', class_='match-item')
all_data = []
for element in match_elements[:30]: # Limit to 30 matches
# Extract match URL
href = element.get('href')
match_url = f'https://www.vlr.gg{href}'
# Extract date
date_div = element.find('div', class_='match-item-date')
date = date_div.text.strip() if date_div else 'N/A'
# Extract team names
team_names = element.find_all('div', class_='match-item-team-name')
team1 = team_names[0].text.strip() if len(team_names) > 0 else 'Unknown'
team2 = team_names[1].text.strip() if len(team_names) > 1 else 'Unknown'
# Extract series scores
score1_span = element.find('span', class_='match-item-score-team1')
score2_span = element.find('span', class_='match-item-score-team2')
score1 = int(score1_span.text.strip()) if score1_span and score1_span.text.strip().isdigit() else 0
score2 = int(score2_span.text.strip()) if score2_span and score2_span.text.strip().isdigit() else 0
winner = team1 if score1 > score2 else team2
# Extract match ID from URL
match_id = href.split('/')[2] if len(href.split('/')) > 2 else 'N/A'
# Get map data from individual match page
maps_info = get_match_data(match_url)
time.sleep(1) # Avoid rate-limiting
# Prepare map data
map_data = {}
for i in range(3):
map_key = f'map{i+1}'
score_key = f'map{i+1}_score'
if i < len(maps_info):
map_data[map_key] = maps_info[i]['name']
map_data[score_key] = maps_info[i]['score']
else:
map_data[map_key] = None
map_data[score_key] = None
# Combine into row
row = {
'match_id': match_id,
'date': date,
'team1': team1,
'team2': team2,
'match_winner': winner
}
row.update(map_data)
all_data.append(row)
# Create DataFrame
columns = [
'match_id', 'date', 'team1', 'team2',
'map1', 'map1_score', 'map2', 'map2_score', 'map3', 'map3_score',
'match_winner'
]
df = pd.DataFrame(all_data, columns=columns)
# Save to CSV
df.to_csv('vlr_matches.csv', index=False)
print("Data saved to 'vlr_matches.csv'")
print(df.head())
print(f"Total rows: {len(df)}")
if __name__ == '__main__':
main()
📁 Output
This script will generate a file named vlr_matches.csv in the current directory with 30 rows, each representing a match, and the following columns:
| match_id | date | team1 | team2 | map1 | map1_score | map2 | map2_score | map3 | map3_score | match_winner |
|---|
| 1234 | 2025-03-01 | Team A | Team B | Ascent | 13-10 | Bind | 7-13 | Null | Null | Team A |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
⚠️ Notes & Considerations
This script provides a robust and structured way to extract and organize Valorant esports match data from vlr.gg.