import requests
from lxml import etree
import json
import pandas
import pygal
from pygal.style import DefaultStyle
chart = pygal.StackedLine(fill = True, interpolate = 'cubic',style = DefaultStyle)
url='https://pvp.qq.com/web201605/js/herolist.json'
page=requests.get(url)
text=page.text
a=json.loads(text)
headers={'User_Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
hero_dict={'生存能力':{},'攻击伤害':{},'技能效果':{},'上手难度':{}}
for i in a:
hero_url='https://pvp.qq.com/web201605/herodetail/{}.shtml'.format(i['ename'])
hero_page=requests.get(url=hero_url,headers=headers)
hero_page.encoding='gbk'
info=etree.HTML(hero_page.text)
name=info.xpath('//h2[@class="cover-name"]')[0].text
sur_info=info.xpath('//ul[@class="cover-list"]/li[1]/span/i/@style')
atk_info = info.xpath('//ul[@class="cover-list"]/li[2]/span/i/@style')
atk1_info = info.xpath('//ul[@class="cover-list"]/li[3]/span/i/@style')
diff_info = info.xpath('//ul[@class="cover-list"]/li[4]/span/i/@style')
hero_dict['生存能力'][name] = sur_info[0][6:-1]
hero_dict['攻击伤害'][name] = atk_info[0][6:-1]
hero_dict['技能效果'][name] = atk1_info[0][6:-1]
hero_dict['上手难度'][name] = diff_info[0][6:-1]
sur_list=[]
atk_list=[]
atk1_list=[]
diff_list=[]
hero_list=[]
for i in hero_dict['生存能力']:
hero_list.append(i)
for k,v in hero_dict.items():
if k=='生存能力':
sur_list.append(int(v[i]))
if k=='攻击伤害':
atk_list.append(int(v[i]))
if k=='技能效果':
atk1_list.append(int(v[i]))
if k=='上手难度':
diff_list.append(int(v[i]))
###柱状图需要的是dataFrame格式 ,不符合
#hr = pandas.DataFrame(hero_dict).transpose()
#chart = pygal.HorizontalBar()
#chart.title = '生存能力'
#for hero in hero_list:
#print(hr[hero])
#for i in range(len(hr[hero].value_counts())):
#chart.add(hr[hero].value_counts().index[i],hr[hero].value_counts().iloc[i],rounded_bars = 5)
#chart.render_to_file('生存能力.svg')
## 折线图是列表数据,并且为int
chart.title = '生存能力'
chart.x_labels = hero_list
chart.add('生存能力',list(sur_list))
chart.add('攻击伤害',list(atk_list))
chart.add('技能效果',list(atk1_list))
chart.add('上手难度',list(diff_list))
chart.render_to_file('test2.svg')
英雄数据分析
最新推荐文章于 2024-08-24 11:00:49 发布