昨天,我做了一个词频统计的小案例。然后,可视化做上瘾了。今天,我吃饭看火影的凯皇励志片段,我想要了解凯皇有多强,接着去网上百度,将凯皇的能力爬取下来,之后可视化出来。我也加入了我欣赏的几个角色,鸣人,我爱罗,佐助。来进行能力值可视化的对比,我发现凯皇确实很强!!
我现在仿佛听见柱间说:“代码!来啊!先上代码啊!!”。好的,上代码:
#encoding="utf-8"
#Author:Mr.Pan_学狂
#Finished_time:2021/3/11/23:43
from selenium import webdriver#selenium库爬取信息
from pyecharts.charts import *#导入所有的图形
from pyecharts import options as opts#导入pyecharts的配置
import random
import time
options = webdriver.ChromeOptions()#配置chrome
options.add_argument('--headless')#将配置添加无头模式
options.add_argument('--disable-gpu')#关闭GPU
driver = webdriver.Chrome(options=options)#导入之前设置的配置
class HY_Persons:#定义类,火影人物
def __init__(self):#初始化函数
self.kh = {}#创建凯皇的字典,存储凯皇的各项能力以及能力值
self.wal = {}#创建我爱罗的字典,存储我爱罗的各项能力以及能力值
self.zz = {}#创建佐助的字典,存储佐助的各项能力以及能力值
self.mr = {}#创建鸣人的字典,存储鸣人的各项能力以及能力值
def Request(self,url):#方便以后使用,一次创建,多次调用
driver.get(url)#使用get请求页面url地址
driver.implicitly_wait(random.randint(3, 5))#设置页面的隐式等待时间
def spyder(self):#定义爬虫
#凯皇,人物能力总和
print("凯皇")
url = 'https://baike.baidu.com/item/%E8%BF%88%E7%89%B9%E5%87%AF/4897951?fr=aladdin#3'#url地址
HY_Persons.Request(self,url)#调用Request函数执行get请求,以及设置响应等待时间
for num in range(1,10):#循环1到9次
talent = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[1]/tbody/tr[1]/th[{}]'.format(num)).text
#使用格式化字符串的方法,将循环得到的数字加入的其中,使得数据不断更新
talent_value = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[1]/tbody/tr[2]/td['+str(num)+']/div').text
#使用字符串拼接的方法,先将数字转换成字符串,再将字符串拼接起来。
#print(talent)#打印能力的各项指标
#print(talent_value)#打印能力值
self.kh[talent] = talent_value#得到凯皇的各项能力以及能力值字典。
print(self.kh)#打印字典
time.sleep(random.randint(1,2))
#爬取佐助,人物能力总和
print('佐助')
url2 = 'https://baike.baidu.com/item/%E5%AE%87%E6%99%BA%E6%B3%A2%E4%BD%90%E5%8A%A9/401898?fromtitle=%E4%BD%90%E5%8A%A9&fromid=286034#3'
HY_Persons.Request(self,url2)
number = 1
for z in range(1,10):
if z == 10:
number = 0
talent = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[1]/tbody/tr[1]/th['+str(z+number)+']/div').text
talent_value = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[1]/tbody/tr[4]/td['+str(z)+']/div').text
self.zz[talent] = talent_value
print(self.zz)
time.sleep(random.randint(1,2))
#爬取鸣人,人物能力总和
print("鸣人")
url3 = 'https://baike.baidu.com/item/%E6%BC%A9%E6%B6%A1%E9%B8%A3%E4%BA%BA/322717?fromtitle=%E9%B8%A3%E4%BA%BA&fromid=54086698'
HY_Persons.Request(self,url3)
for m in range(1,10):
if m == 10:
number = 0
talent = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[2]/tbody/tr[1]/th['+str(m+number)+']/div').text
talent_value = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[2]/tbody/tr[4]/td['+str(m)+']/div').text
self.mr[talent] = talent_value
print(self.mr)
self.mr['总计'] = '52'
print(self.mr)
time.sleep(random.randint(1,2))
#爬取我爱罗,人物能力总和
print("我爱罗")
url4 = "https://baike.baidu.com/item/%E6%88%91%E7%88%B1%E7%BD%97/237248#2"
HY_Persons.Request(self,url4)
for w in range(1,10):
if w == 10:
number = 0
talent = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[1]/tbody/tr[1]/th['+str(w+number)+']/div').text
talent_value = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/table[1]/tbody/tr[4]/td['+str(w)+']/div').text
self.wal[talent] = talent_value
print(self.wal)
def Data_show(self):
# dt = {"name": "", "max": "10", "min": "0"}
# data_schema = []
# for d in self.zz.keys():
# dt["name"] = d
# print(dt)
# data_schema.append(dt)
# print(data_schema)
kh = []
zz = []
mr = []
wal = []
self.kh['智力']=10#将智力和力量的值互换,保证跟后面的数据一致性
self.kh['力量']=6#是可视化的数据是正确的
for d in self.kh.values():#凯皇的能力值
kh.append(d)
value_kh = [kh]#存储凯皇能力值
print(value_kh)
for z in self.zz.values():#佐助的能力值
zz.append(z)#存储佐助的能力值
value_zz = [zz]
for m in self.mr.values():#鸣人的能力值
mr.append(m)#存储鸣人的能力值
value_mr = [mr]
for w in self.wal.values():#我爱罗的能力值
wal.append(w)#存储我爱罗的能力值
value_wal = [wal]
data_schema = [{"name":"忍术","max":"10","min":"0"},#雷达图的各个能力数据。
{"name":"体术","max":"10","min":"0"},
{"name":"幻术","max":"10","min":"0"},
{"name":"智慧","max":"10","min":"0"},
{"name":"力量","max":"10","min":"0"},
{"name":"速度","max":"10","min":"0"},
{"name":"查克拉","max":"10","min":"0"},
{"name":"结印","max":"10","min":"0"},
{"name":"总计","max":"80","min":"0"}]
Rdr = (
Radar()
.add_schema(schema=data_schema,shape="circle")
.add("凯皇",value_kh,color="#DC143C")#设置红色
.add("佐助",value_zz,color="#9400D3")#设置紫色
.add("鸣人",value_mr,color="#FFA500")#设置橘黄色
.add("我爱罗",value_wal,color="#4fa5e1")#设置蓝色
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="火影人物可视化")#设置标题
)
.render('F:/Radar.html')#另存为F盘下面
)
if __name__ == '__main__':#定义主函数,程序入口
HY = HY_Persons()#实例化对象
HY.spyder()#调用函数
HY.Data_show()#调用函数,可视化
代码的解释在注释中有了,不懂的朋友可以私信我一起探讨。
运行结果,如下图:
第一次做雷达图,没有经验,感觉很好看。后续有时间,会再做可视化的案例。
最后,感谢大家前来观看鄙人的文章,文中或有诸多不妥之处,还望指出和海涵。