对足球分析感兴趣?

DDD
发布: 2024-09-14 06:19:32
原创
1232 人浏览过

我最近开始了我的足球分析之旅,并创建了一个示例 Python 程序,该程序引用 https://understat.com/ 来抓取单场比赛的射门数据。

这标志着我数据操作之旅的开始。我很高兴能更深入地研究这个领域,并期待随着我的进步分享更多更新。

回购:
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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!