使用python爬取詹姆斯生涯数据储存到csv文件中并可视化
时间: 2024-06-09 09:11:11 浏览: 110
好的,你可以使用Python中的Requests和BeautifulSoup库进行网页数据的爬取,使用Pandas库将数据存储到CSV文件中,并使用Matplotlib库进行数据可视化。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
# 爬取网页数据
url = 'https://www.basketball-reference.com/players/j/jamesle01.html'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 提取数据
table = soup.find('table', {'id': 'per_game'})
headers = [th.getText() for th in table.findAll('tr')[0].findAll('th')]
rows = table.findAll('tr')[1:]
data = [[td.getText() for td in rows[i].findAll('td')] for i in range(len(rows))]
# 存储到CSV文件
df = pd.DataFrame(data, columns=headers[1:])
df.to_csv('lebron_james.csv', index=False)
# 数据可视化
df['PTS'] = df['PTS'].astype(float)
df['MP'] = df['MP'].astype(float)
df.plot(kind='line', x='Year', y='PTS', figsize=(10, 5))
plt.title('LeBron James Career Points')
plt.xlabel('Year')
plt.ylabel('Points')
plt.show()
```
运行代码后,将会在同级目录下生成一个名为lebron_james.csv的文件,并显示出数据可视化图表。
阅读全文