python爬取詹姆斯职业生涯数据并生成图标展示
从网页获取数据
使用plt生成柱状图
import requests
from fake_useragent import UserAgent
from lxml import html
import matplotlib.pyplot as plt
import numpy as np
def get_data():
url = 'http://www.stat-nba.com/player/1862.html'
response = requests.get(url, headers={'User-Agent': UserAgent().random})
etree = html.etree
e = etree.HTML(response.text)
scores = e.xpath('//table[@id="stat_box_avg"]//tr//td[24]/text()')
backboards = e.xpath('//table[@id="stat_box_avg"]//tr//td[16]/text()')
assists = e.xpath('//table[@id="stat_box_avg"]//tr//td[19]/text()')
times = e.xpath('//*[@id="stat_box_avg"]/tbody/tr/td[2]/a/text()')
del (scores[-1])
del (backboards[-1])
del (assists[-1])
for i in range(0, 17):
scores[i] = float(scores[i])
backboards[i] = float(backboards[i])
assists[i] = float(assists[i])
scores.reverse()
backboards.reverse()
scores.reverse()
assists.reverse()
times.reverse()
return scores, backboards, assists, times
def create_chart(scores, backboards, assists, times):
print(scores)
year = 17
total_width = 0.9
n = 3
width = total_width / n
x = np.arange(year)
x = x - (total_width - width) / 2
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei', 'Times New Roman']
plt.bar(x, backboards, width=width, label="篮板")
plt.bar(x + width, assists, width=width, label="助攻")
plt.bar(x + 2 * width, scores, width=width, label="得分")
print(x + 2 * width)
print(scores)
print(type(x + 2 * width))
print(type(scores))
# 设置横坐标
plt.xticks(x + width, times, rotation=50)
# 显示图注
plt.legend()
# 设置标题
plt.title('勒布朗·詹姆斯所有赛季数据对比')
# 设置y轴区间
# plt.ylim(0, 35)
# 设置y轴文字
# plt.set_ylabel("数据")
# 显示图表
plt.show()
def main():
scores, backboards, assists, times = get_data()
create_chart(scores, backboards, assists, times)
if __name__ == '__main__':
main()
效果图