吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4867|回复: 19
收起左侧

[Python 原创] 【python】利用python爬虫 将LOL所有英雄的技能介绍给爬取下来

  [复制链接]
tomerror 发表于 2018-11-15 14:25
本帖最后由 tomerror 于 2018-11-15 23:28 编辑

工欲善其事  必先利其器

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

运行环境

python 3.7

此爬虫所用的库有

  1. requests (获取网页信息)
  2. openpyxl (Excel相关操作)
  3. pymysql (MySQL数据库相关操作)
  4. 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版:
ivSn76.jpg
MySQL版:
ivSm0x.jpg
下面是上文代码打包为EXE版(pyinstaller)
主文件(Excel版):https://www.lanzouj.com/i2dnmvg
主文件(MySQL版):https://www.lanzouj.com/i2dnmej
数据文件:https://www.lanzouj.com/i2dnn9a  (必要文件,运行主文件时要读取他的信息。或者可以运行下面的 获取数据文件 进行自动生成)
获取数据文件:https://www.lanzouj.com/i2dnm7c  (运行后会自动生成最新的数据文件)

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

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hf2008paopao 发表于 2018-11-15 14:57
tomerror 发表于 2018-11-15 14:45
python的相关课程 论坛里有很多呀 搜一下就有了

如:

我的意思你可能没理解,我知道论坛课程很多,我说的意思是,新手应该按照什么样的顺序来学习,这样学出来才能得以所用,我学python 想做做工程师。爬虫之类的,python后期如何自主赚钱,还是学会只能去对接企业呢
 楼主| tomerror 发表于 2018-11-15 16:22
本帖最后由 tomerror 于 2018-11-15 16:31 编辑
xuanqi521 发表于 2018-11-15 16:04
问下楼主,爬出来的如下列表形式的内容,怎么用py有序整理后放入excel

[Python] 纯文本查看 复制代码
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')

我给解释一下我这段代码的意思吧  你就懂了
第二行 openpyxl.Workbook() 创建工作表
第三行 wb.active 获取工作表
最后一行 wb.save('herotest.xlsx') 保存为herotest.xlsx文件

然后我的her里的内容是  [[a的英雄称号,a的英雄名称,a的被动技能,a的主动技能],[b的英雄称号,b的英雄名称,b的被动技能,b的主动技能],[c的英雄称号,c的英雄名称,c的被动技能,c的主动技能]]
就是 [[x,x,x,x],[x,x,x,x],[x,x,x,x]]

我用的的库是 openpyxl
openpyxl他还能画图 画表等 想要深入学习的话 可以去看看官方文档 我学这个库不是好深
hf2008paopao 发表于 2018-11-15 14:33
有没有python的系统课程呢?从基础到实战那种想自学下。
 楼主| tomerror 发表于 2018-11-15 14:45
hf2008paopao 发表于 2018-11-15 14:33
有没有python的系统课程呢?从基础到实战那种想自学下。

python的相关课程 论坛里有很多呀 搜一下就有了

如:
Pythone新手学习导图 https://www.52pojie.cn/thread-806162-1-1.html (出处: 吾爱破解论坛)
分享老男孩Python全栈9期全套(共131天)https://www.52pojie.cn/thread-799691-1-1.html (出处: 吾爱破解论坛)
等等 有很多的

因为python很"庞大",很多事都能做,所以学之前要明确自己的学习方向
Kaiter_Plus 发表于 2018-11-15 15:05
感谢楼主分享!
 楼主| tomerror 发表于 2018-11-15 15:17
hf2008paopao 发表于 2018-11-15 14:57
我的意思你可能没理解,我知道论坛课程很多,我说的意思是,新手应该按照什么样的顺序来学习,这样学出来 ...

哦 你要系统的学习哦 那最简单的就是去各大学习平台报班,他们有系统的培训
如果自学,那就去这些网站里看看他们的学习课程里都要学习些啥  这些一般在他们课程简介里都有
jiazhilin1997 发表于 2018-11-15 15:17
6呀兄弟               
hf2008paopao 发表于 2018-11-15 15:26
tomerror 发表于 2018-11-15 15:17
哦 你要系统的学习哦 那最简单的就是去各大学习平台报班,他们有系统的培训
如果自学,那就去这些网站里 ...

嗯嗯,好的。谢谢
wangqiustc 发表于 2018-11-15 15:28
ig牛逼,楼主更厉害
leyn520 发表于 2018-11-15 15:29
厉害,我也想学习下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-21 23:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表