>  기사  >  백엔드 개발  >  축구 분석에 관심이 있으십니까?

축구 분석에 관심이 있으십니까?

DDD
DDD원래의
2024-09-14 06:19:32937검색

저는 최근 축구 분석에 대한 탐구를 시작했으며 단일 게임 샷 데이터를 스크랩하기 위해 https://understat.com/을 참조하는 샘플 Python 프로그램을 만들었습니다.

이것은 데이터 조작에 대한 나의 여정의 시작을 의미합니다. 이 분야에 대해 더 깊이 탐구하게 되어 기쁘게 생각하며 진행하면서 더 많은 업데이트를 공유할 수 있기를 기대합니다.

저장소:
https://github.com/UribeJr/football-data-scraper-to-csv-exporter

#!/usr/bin/env python
# coding: utf-8

# In[2]:


#import modules and packages
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd


# In[3]:


#scrape single game shots
base_url = 'https://understat.com/match/'
match = str(input("Enter your match ID: "))
url = base_url + match


# In[16]:


res = requests.get(url)
soup = BeautifulSoup(res.content, 'lxml')
span = soup.find('span')
script = soup.find_all('script')
script


# In[18]:


string = script[1].string
string


# In[26]:


#strip symbols so we only have json data
index_start = string.index("('") + 2
index_end = string.index("')")

json_data = string[index_start:index_end]
json_data = json_data.encode('utf8').decode('unicode_escape')
data = json.loads(json_data)


# In[35]:


df_h = pd.DataFrame(data['h'])
print("Home Team DataFrame:")
print(df_h.head())


# In[37]:


# Save the home team DataFrame to a CSV file
df_h.to_csv('home_team_shots.csv', index=False)


# In[ ]:

방법

  • 필요한 모든 패키지/모듈 요청, pandas, BeautifulSoup 가져오기
  • https://understat.com/으로 이동하여 특정 샷 데이터를 원하는 경기로 이동하세요. 일치 URL은 다음과 같아야 합니다. https://understat.com/match/{match-id}
  • data_scraping.py를 실행하고 match-id를 입력하세요

축하해요!

그런 다음 프로그램은 경기에서 샷 데이터를 스크랩하고 각 홈 및 어웨이 팀 데이터를 별도의 데이터 프레임으로 변환합니다. 그런 다음 데이터 프레임은 참조용으로 별도의 CSV 파일로 내보내집니다.

데이터 프레임:

Interested in Football Analytics?

CSV:

Interested in Football Analytics?

위 내용은 축구 분석에 관심이 있으십니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.