工欲善其事  必先利其器

要想玩好LOL,那了解所有英雄的技能必然是其最基本的。所以此爬虫就应运而生

运行环境

python 3.7

此爬虫所用的库有

requests (获取网页信息)

openpyxl (Excel相关操作)

pymysql (MySQL数据库相关操作)

re (正则)

代码

下面有已打包为EXE的程序,可直接使用

主要代码

import requests

import re

import openpyxl

import pymysql

def get_html(hero):

headers = {

'Referer': 'http://lol.qq.com/web201310/info-defail.shtml?id=' + hero,

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',

}

url = 'http://lol.qq.com/biz/hero/' + hero + '.js'

# print(url)

response = requests.get(url, headers=headers)

response = response.text

return response

def hero_info(response):

# 英雄名称

hero_name = re.findall(r'"name":"(.*?)","title"', response, re.S)[0]

hero_title = re.findall(r'"title":"(.*?)","tags"', response, re.S)[0]

# 技能(QWER)

hero_spells = re.findall(r'"spells":(.*?),"passive"', response, re.S)[0]

# 技能名称

hero_spells_name = re.findall(

r'"name":"(.*?)","description"', hero_spells, re.S)

# 技能描述

hero_spells_description = re.findall(

r'"description":"(.*?)","image"', hero_spells, re.S)

# 技能消耗

hero_spells_resource = re.findall(

r'"resource":"(.*?)"}', hero_spells, re.S)

# 技能主被动

hero_spells_group = re.findall(r'"group":"(.*?)","x"', hero_spells, re.S)

spells_Q = hero_spells_name[0] + ':' + hero_spells_description[0] + \

'|' + hero_spells_resource[0] + '|' + hero_spells_group[0]

spells_W = hero_spells_name[1] + ':' + hero_spells_description[1] + \

'|' + hero_spells_resource[1] + '|' + hero_spells_group[1]

spells_E = hero_spells_name[2] + ':' + hero_spells_description[2] + \

'|' + hero_spells_resource[2] + '|' + hero_spells_group[2]

spells_R = hero_spells_name[3] + ':' + hero_spells_description[3] + \

'|' + hero_spells_resource[3] + '|' + hero_spells_group[3]

Spells = spells_Q + '\n' + spells_W + '\n' + spells_E + '\n' + spells_R

# 被动技能

hero_passive = re.findall(r'"passive":(.*?),"lore"', response, re.S)[0]

# 被动技能名称

hero_passive_name = re.findall(

r'"name":"(.*?)","description"', hero_passive, re.S)[0]

# 技能描述

hero_passive_description = re.findall(

r'"description":"(.*?)","image"', hero_passive, re.S)[0]

# 技能主被动

hero_passive_group = re.findall(

r'"group":"(.*?)","x"', hero_passive, re.S)[0]

passive = hero_passive_name + ':' + \

hero_passive_description + '|' + hero_passive_group

hero_spells_info = [hero_name, hero_title, passive, Spells]

return hero_spells_info

def get_hero():

with open('hero', 'r') as f:

hero = f.readlines()

return hero

def save_to_excel(her):

wb = openpyxl.Workbook()

ws = wb.active

ws['A1'] = '英雄称号'

ws['B1'] = '英雄名称'

ws['C1'] = '被动技能'

ws['D1'] = '主动技能'

for hero in her:

ws.append(hero)

wb.save('herotest.xlsx')

def save_to_mysql(her):

for i in her:

ch = '"' + i[0] + '"'

name = '"' + i[1] + '"'

bd_name = '"' + i[2] + '"'

zd_name = '"' + i[3] + '"'

db = pymysql.connect(host='localhost', user='root',

password='123456', database='python_mysql', charset='utf8')

cursor = db.cursor()

sql = ''' insert into lolheroinfo values (%s, %s, %s, %s);

''' % (ch, name, bd_name, zd_name)

# print(sql)

try:

# 执行sql语句

cursor.execute(sql)

# 提交到数据库执行

db.commit()

print(ch, ' insert into success!')

except:

db.rollback()

db.close()

return True

def main():

heros = get_hero()

her = []

for hero in heros:

hero = hero.split('"')[3]

response = get_html(hero)

her_infos = hero_info(response)

her_encode = []

for i in her_infos:

i = i.encode("latin-1").decode("unicode_escape")

her_encode.append(i)

her.append(her_encode)

save_to_excel(her)

#save_to_mysql(her)

if __name__ == '__main__':

main()

获取英雄数据文件

运行此文件后会在当前目录下生产hero的数据文件

import requests

import re

headers = {

'Referer': 'https://lol.qq.com/data/info-defail.shtml?id=Aatrox',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',

}

response = requests.get('https://lol.qq.com/biz/hero/champion.js', headers=headers)

keys = re.findall(r'"keys":{(.*?)},"data"',response.text,re.S)

keys = keys[0]

keys = keys.split(',')

with open('hero','w') as f:

for key in keys:

f.write(key)

f.write('\n')

print(key)

_如果想要保存到MySQL,请先创建MySQL数据库,然后用下面的代码创建表,最后取消主代码save_tomysql(her)的注释即可

创建数据表代码

#!/usr/bin/python3

import pymysql

# 打开数据库连接

db = pymysql.connect("localhost","root","123456","python_mysql" )

# 使用 cursor() 方法创建一个游标对象 cursor

cursor = db.cursor()

# 使用 execute() 方法执行 SQL,如果表存在则删除

cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

# 使用预处理语句创建表

sql = """CREATE TABLE lolheroinfo (

英雄称号  CHAR(255),

英雄名称 CHAR(255),

被动技能 CHAR(255),

主动技能 varchar(999)

)

"""

cursor.execute(sql)

# 关闭数据库连接

db.close()

Excel版:

MySQL版:

下面是上文代码打包为EXE版(pyinstaller)

主文件(Excel版):https://www.lanzous.com/i2dnmvg

主文件(MySQL版):https://www.lanzous.com/i2dnmej

数据文件:https://www.lanzous.com/i2dnn9a  (必要文件,运行主文件时要读取他的信息。或者可以运行下面的 获取数据文件 进行自动生成)

获取数据文件:https://www.lanzous.com/i2dnm7c  (运行后会自动生成最新的数据文件)

最后温馨提示请合理使用爬虫

python技能描述_【python】利用python爬虫 将LOL所有英雄的技能介绍给爬取下来相关推荐

  1. python爬虫入门------王者荣耀英雄及皮肤数据爬取项目

    王者荣耀英雄及皮肤数据爬取项目 一:做前需知 笔者这段学习了一些爬虫的知识,然后做了一个小项目.接下来,我会把项目的流程展示出来. 运行环境:python 3.6.3.pycharm 2019-3-3 ...

  2. ios安装python的步骤_如何利用 Python 爬虫实现给微信群发新闻早报?(详细)

    点击上方"AirPython",选择"加为星标" 第一时间关注 Python 技术干货! 1. 场景 经常有小伙伴在交流群问我,每天的早报新闻是怎么获取的? 其 ...

  3. 怎么用python找论文_如何利用Python绘制学术论文图表方法

    论文中图表形式多样,常用的处理工具有excel.MATLAB以及Python等,excel自处理的方法有两个缺陷: 1.当数据较多时,容易出现excel"翻白眼"的现象: 2.需要 ...

  4. 利用python进行数据分析_资料 | 利用Python进行数据分析

    下载地址:https://www.yanxishe.com/resourceDetail/1443?from=leiphonecolumn_res0518 以下内容节选自豆瓣: 内容简介 · · · ...

  5. python求加速度_如何利用Python 为自然语言处理加速度

    自去年发布 Python 的指代消解包(coreference resolution package)之后,很多用户开始用它来构建许多应用程序,而这些应用与我们最初的对话应用完全不同. 利用 spaC ...

  6. python数据论文_如何利用Python绘制学术论文图表

    论文中图表形式多样,常用的处理工具有excel.MATLAB以及Python等,excel自处理的方法有两个缺陷:1.当数据较多时,容易出现excel"翻白眼"的现象:2.需要使用 ...

  7. python特性描述_详解 Python 最优雅的特性之一 — 描述符

    本篇选自 Python黑魔法指南 -> 第四章 -> 第2节. github仓库: https://github.com/iswbm/magic-python magic-python 目 ...

  8. 用python做头像_如何利用python制作微信好友头像照片墙?

    这个不难,主要用到itchat和pillow这2个库,其中itchat用于获取微信好友头像照片,pillow用于拼接头像生成一个照片墙,下面我简单介绍一下实现过程,代码量不多,也很好理解,实验环境wi ...

  9. python epub 精品_如何利用Python打包HTML页面为epub?

    最近沉迷于将各种博客和官方文档html转化成pdf,结果用手机看还是不太方便,所以想到将html转化成epub格式的电子书,要用os,re,requests,lxm,zipfile,五个库,在这里分享 ...

最新文章

  1. Django(part33)--数据库的迁移
  2. 如何给Android添加可视化工具,可视化实现在手机上抓包 方便调试 OkNetworkMonitor...
  3. 用ElasticSearch存储日志
  4. 文本框仅可接收decimal
  5. 图片呈现jQuery中fadeIn、fadeOut、fadeTo的用法(图片隐藏与显示)
  6. 统计一个子字符串在另一个字符串中出现的次数
  7. 学生用计算机怎么乘跟跟号号,3根号键的啊函数计算器价格多少,它与普通计 – 手机爱问...
  8. 黑苹果之驱动小米随身WiFi
  9. Linux:MLX90614驱动
  10. 数据分析——ETF基金申购赎回清单
  11. 安卓第三方支付之微信支付
  12. 虚拟化技术介绍 hypervisor简介
  13. OBS Studio 录制视频画面很糊,不清晰,如何解决?
  14. left join 和 left outer join (可解决多个表left join的问题)
  15. 基于CNN的2D多人姿态估计论文综述
  16. 成电计算机考研专业课,2020成电软件工程考研经验贴
  17. Python 数据挖掘之电力窃漏电用户自动识别
  18. Spring为什么需要使用三级缓存?
  19. HDU1869:六度分离
  20. STM32F系列ARM Cortex-M3核微控制器基础之系统时钟一

热门文章

  1. BSP Day 24
  2. 输入字符串和遍历字符数组的方法
  3. 在海思和自己的板子上运行HI3519AV100 sample_venc
  4. 羊了个羊第二关“真实”攻略
  5. 企业做软文推广文章没有图片是硬伤
  6. 车路协同实现第二次技术革新 觉非科技路侧融合感知系统知寰™正式发布
  7. 输入八进制数,输出十进制数(C语言)
  8. Apple ID密保问题是个梗之重置密保成功终极方案
  9. java 统计qps_统计接口QPS
  10. [DForm]我也来做自定义Winform之另类标题栏重绘