python爬取詹姆斯职业生涯数据并生成图标展示
2021/7/19 1:06:42
本文主要是介绍python爬取詹姆斯职业生涯数据并生成图标展示,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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()
效果图
这篇关于python爬取詹姆斯职业生涯数据并生成图标展示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享