使用requests库、beautifuisoup4库爬取hupa球员的排名 球员 球队 得分 命中-出手 命中率 命中-三分 三分命中率 命中-罚球 罚球命中率 场次,通过openpyxl库存储到excel文件中,使用cat,workbook,load_work,并选择合适的图表将前20名球员的数据呈现。
时间: 2024-03-04 08:52:32 浏览: 144
好的,我可以帮你实现这个功能。
首先,我们需要发送 HTTP 请求获取球员排名页面的 HTML 内容,并使用 BeautifulSoup 库解析 HTML,提取出球员的相关信息。代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.stat-nba.com/ranking/player/pts-1.html'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'class': 'stat_box'})
rows = table.find_all('tr')[1:]
data = []
for row in rows:
items = row.find_all('td')
player = items[0].text.strip()
team = items[1].text.strip()
points = float(items[2].text.strip())
fgm_a = items[3].text.strip()
fgm_a = fgm_a.split('-')
fgm = float(fgm_a[0])
fga = float(fgm_a[1])
fg_pct = float(items[4].text.strip())
threes = items[5].text.strip()
threes = threes.split('-')
threes_made = float(threes[0])
threes_attempted = float(threes[1])
three_pct = float(items[6].text.strip())
ftm_a = items[7].text.strip()
ftm_a = ftm_a.split('-')
ftm = float(ftm_a[0])
fta = float(ftm_a[1])
ft_pct = float(items[8].text.strip())
games = int(items[9].text.strip())
data.append((player, team, points, fgm, fga, fg_pct, threes_made, threes_attempted, three_pct, ftm, fta, ft_pct, games))
```
然后,我们可以使用 openpyxl 库创建一个 Excel 文件,并将数据存储到工作表中。代码如下:
```python
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.append(['球员', '球队', '得分', '命中', '出手', '命中率', '三分命中', '三分出手', '三分命中率', '罚球命中', '罚球出手', '罚球命中率', '场次'])
for item in data:
ws.append(item)
wb.save('players.xlsx')
```
最后,我们可以使用 openpyxl 库读取 Excel 文件中的数据,并使用 matplotlib 库绘制图表。代码如下:
```python
import matplotlib.pyplot as plt
from openpyxl import load_workbook
wb = load_workbook('players.xlsx')
ws = wb.active
data = []
for row in ws.iter_rows(min_row=2, max_row=21, values_only=True):
player, team, points, fgm, fga, fg_pct, threes_made, threes_attempted, three_pct, ftm, fta, ft_pct, games = row
data.append((player, points, fg_pct, three_pct, ft_pct))
labels = [item[0] for item in data]
points = [item[1] for item in data]
fg_pcts = [item[2] for item in data]
three_pcts = [item[3] for item in data]
ft_pcts = [item[4] for item in data]
fig, ax = plt.subplots(figsize=(10, 8))
ax.bar(labels, points)
ax.set_xlabel('球员')
ax.set_ylabel('得分')
ax.set_title('前20名球员得分排名')
plt.xticks(rotation=90)
plt.show()
```
这段代码会生成一个柱状图,显示前20名球员的得分排名。你可以根据需要选择合适的图表类型,并修改代码以显示其他数据。
希望这些信息能够对你有所帮助!
阅读全文