Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrap Football data table of previous matches from the official football data websites (fbref...) #1076

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Football site webiste table scrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#LA LIGA SQUAD STATS TABLE#


import requests
import pandas as pd
from bs4 import BeautifulSoup

# Define the URL of the webpage you want to scrape
url = "https://fbref.com/en/squads/8d6fd021/Alaves-Stats"

# Send an HTTP GET request to the URL
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
# Parse the HTML content of the page
soup = BeautifulSoup(response.text,'html.parser')
squad_stats_table = soup.select('table.stats_table')[2]

if squad_stats_table:
# Extract data directly from the table
data = []
for row in squad_stats_table.find_all('tr')[1:]:
row_data = [cell.get_text(strip=True) for cell in row.find_all(['td', 'th'])]
data.append(row_data)

# Define the column names (headers)
headers = data[0]
# Create a pandas DataFrame
df = pd.DataFrame(data[1:], columns=headers)

# Save the DataFrame to an Excel file
excel_file = "Team Score .xlsx"
df.to_excel(excel_file, index=False)

print(f"Data has been scraped and saved to {excel_file}")
else:
print("Squad stats table not found on the page.")
else:
print("Failed to retrieve the webpage")