【scrapy爬虫实战】王者荣耀全部英雄信息爬取

王者荣耀英雄信息爬取

分析

入口页面地址

https://pvp.qq.com/web201605/herolist.shtml

第一步获取所有英雄的列表

image-20200525100704049.png

可以看到英雄列表是在源码中可以被找到的


image-20200525100755019.png

第二步 获取英雄的各种信息

英雄的基本信息放在一个class = "cover"的div中 我们主要采集英雄的名称技能介绍

image-20200525101942363.png

技能部分都在 class=" zk-con3 zk-con" 中 中的 ul

image-20200525103312535.png

爬取英雄列表

创建工程

scrapy startproject wzry
cd wzry

创建爬虫

scrapy genspider wzry_spider pvp.qq.com

修改配置

# 不遵循robots协议 因为站点没有这个文件 爬虫会直接略过
ROBOTSTXT_OBEY = False
# 添加请求头
DEFAULT_REQUEST_HEADERS = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}

# 下载延迟
DOWNLOAD_DELAY = 1

修改初始页面

start_urls = ['https://pvp.qq.com/web201605/herolist.shtml']

创建爬取列表方法(测试)

    def parse(self, response):
        print("=" * 50)
        print(response)
        print("=" * 50)

运行爬虫

scrapy crawl wzry_spider
2020-05-25 11:08:53 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-05-25 11:08:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://pvp.qq.com/web201605/herolist.shtml> (referer: None)
==================================================
<200 https://pvp.qq.com/web201605/herolist.shtml>
==================================================
2020-05-25 11:08:54 [scrapy.core.engine] INFO: Closing spider (finished)
2020-05-25 11:08:54 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 315,

成功返回结果

爬取所有英雄链接方法

class WzrySpiderSpider(scrapy.Spider):
    name = 'wzry_spider'
    allowed_domains = ['pvp.qq.com']
    start_urls = ['https://pvp.qq.com/web201605/herolist.shtml']    # 起始url
    base_url = "https://pvp.qq.com/web201605/"  # url 前缀

    def parse(self, response):
        print("=" * 50)
        # print(response.body)
        hero_list = response.xpath("//ul[@class='herolist clearfix']//li")
        for hero in hero_list:
            url = self.base_url + hero.xpath("./a/@href").get()
            print(url)
            # yield scrapy.Request(url)
        print("=" * 50)
https://pvp.qq.com/web201605/herodetail/114.shtml
https://pvp.qq.com/web201605/herodetail/113.shtml
https://pvp.qq.com/web201605/herodetail/112.shtml
https://pvp.qq.com/web201605/herodetail/111.shtml
https://pvp.qq.com/web201605/herodetail/110.shtml
https://pvp.qq.com/web201605/herodetail/109.shtml
https://pvp.qq.com/web201605/herodetail/108.shtml
https://pvp.qq.com/web201605/herodetail/107.shtml
https://pvp.qq.com/web201605/herodetail/106.shtml
https://pvp.qq.com/web201605/herodetail/105.shtml
==================================================

爬取英雄详情页面

  • 获得基本信息
# 基本信息块
hero_info = response.xpath("//div[@class='cover']")
hero_name = hero_info.xpath(".//h2[@class='cover-name']/text()").get()
print(hero_name)
# 分类
sort_num = hero_info.xpath(".//span[@class='herodetail-sort']/i/@class").get()[-1:]
print(sort_num)
# 生存能力
viability = hero_info.xpath(".//ul/li[1]/span/i/@style").get()[6:]
print("生存能力:" + viability)
# 伤害
aggressivity = hero_info.xpath(".//ul/li[2]/span/i/@style").get()[6:]
print("攻击能力:" + aggressivity)
effect = hero_info.xpath(".//ul/li[3]/span/i/@style").get()[6:]
print("技能影响" + effect)
difficulty = hero_info.xpath(".//ul/li[4]/span/i/@style").get()[6:]
print("上手难度:" + difficulty)
  • 获取技能信息
 skill_list = response.xpath("//div[@class='skill-show']/div[@class='show-list']")

        for skill in skill_list:
            skill_name = skill.xpath("./p[@class='skill-name']/b/text()").get()
            if not skill_name:
                continue
            # 冷却时间
            cooling = skill.xpath("./p[@class='skill-name']/span[1]/text()").get().split(":")[1].strip().split('/')
            # 消耗
            consume = skill.xpath("./p[@class='skill-name']/span[1]/text()").get().split(":")[1].strip().split('/')
            # "".strip()
            # 如果这个技能是空的就 continue

            # 技能介绍
            skill_desc = skill.xpath("./p[@class='skill-desc']/text()").get()
            new_skill = {
                "name": skill_name,
                "cooling": cooling,
                "consume": consume,
                "desc": skill_desc
            }

                new_hero = HeroInfo(name=hero_name,
                            sort_num=sort_num,
                            viability=viability,
                            aggressivity=aggressivity,
                            effect=effect,
                            difficulty=difficulty,
                            skills_list=new_skill)

                yield new_hero

        
  • 英雄信息item类
class HeroInfo(scrapy.Item):
    # 存字符串
    name = scrapy.Field()
    sort_num = scrapy.Field()
    viability = scrapy.Field()
    aggressivity = scrapy.Field()
    effect = scrapy.Field()
    difficulty = scrapy.Field()
    # 存字典
    skills_list = scrapy.Field()
  • 储存 pipelines.py文件

需要修改 ITEM_PIPELINES 配置 加入这个处理类

# -*- coding: utf-8 -*-

from scrapy.exporters import JsonItemExporter


class WzryPipeline:
    def __init__(self):
        # 打开文件并实例化JsonItemExporter
        self.fp = open('result.json', 'wb')
        self.save_json = JsonItemExporter(self.fp, encoding="utf-8", ensure_ascii=False, indent=4)
        # 开始写入
        self.save_json.start_exporting()

    def open_spider(self, spider):
        pass

    def close_spider(self, spider):
        # 结束写入
        self.save_json.finish_exporting()
        # 关闭文件
        self.fp.close()

    def process_item(self, item, spider):
        # 写入item
        self.save_json.export_item(item)
        return item

运行爬虫

scrapy crawl wzry_spider

结果

image-20200525150137203.png

全部代码

wzry_spider.py

# -*- coding: utf-8 -*-
import scrapy
from wzry.items import HeroInfo


class WzrySpiderSpider(scrapy.Spider):
    name = 'wzry_spider'
    allowed_domains = ['pvp.qq.com']
    start_urls = ['https://pvp.qq.com/web201605/herolist.shtml']  # 起始url
    base_url = "https://pvp.qq.com/web201605/"  # url 前缀

    def parse(self, response):
        # print(response.body)
        hero_list = response.xpath("//ul[@class='herolist clearfix']//li")
        for hero in hero_list:
            url = self.base_url + hero.xpath("./a/@href").get()
            yield scrapy.Request(url, callback=self.get_hero_info)

    def get_hero_info(self, response):
        # 基本信息块
        global new_skill
        hero_info = response.xpath("//div[@class='cover']")
        hero_name = hero_info.xpath(".//h2[@class='cover-name']/text()").get()

        # 分类
        sort_num = hero_info.xpath(".//span[@class='herodetail-sort']/i/@class").get()[-1:]

        # 生存能力
        viability = hero_info.xpath(".//ul/li[1]/span/i/@style").get()[6:]
        # 伤害
        aggressivity = hero_info.xpath(".//ul/li[2]/span/i/@style").get()[6:]
        effect = hero_info.xpath(".//ul/li[3]/span/i/@style").get()[6:]
        difficulty = hero_info.xpath(".//ul/li[4]/span/i/@style").get()[6:]
        # 技能列表
        skill_list = response.xpath("//div[@class='skill-show']/div[@class='show-list']")

        for skill in skill_list:
            skill_name = skill.xpath("./p[@class='skill-name']/b/text()").get()
            if not skill_name:
                continue
            # 冷却时间
            cooling = skill.xpath("./p[@class='skill-name']/span[1]/text()").get().split(":"
                                                                                         "")[1].strip().split('/')
            # 消耗
            consume = skill.xpath("./p[@class='skill-name']/span[1]/text()").get().split(":"
                                                                                         "")[1].strip().split('/')
            # "".strip()
            # 如果这个技能是空的就 continue

            # 技能介绍
            skill_desc = skill.xpath("./p[@class='skill-desc']/text()").get()
            new_skill = {
                "name": skill_name,
                "cooling": cooling,
                "consume": consume,
                "desc": skill_desc
            }

        new_hero = HeroInfo(name=hero_name,
                            sort_num=sort_num,
                            viability=viability,
                            aggressivity=aggressivity,
                            effect=effect,
                            difficulty=difficulty,
                            skills_list=new_skill)

        yield new_hero

items.py

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class WzryItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass


class HeroInfo(scrapy.Item):
    # 存字符串
    name = scrapy.Field()
    sort_num = scrapy.Field()
    viability = scrapy.Field()
    aggressivity = scrapy.Field()
    effect = scrapy.Field()
    difficulty = scrapy.Field()
    # 存字典
    skills_list = scrapy.Field()

pipelines.py


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.exporters import JsonItemExporter


class WzryPipeline:
    def __init__(self):
        self.fp = open('result.json', 'wb')
        self.save_json = JsonItemExporter(self.fp, encoding="utf-8", ensure_ascii=False, indent=4)
        self.save_json.start_exporting()

    def open_spider(self, spider):
        pass

    def close_spider(self, spider):
        self.save_json.finish_exporting()
        self.fp.close()

    def process_item(self, item, spider):
        self.save_json.export_item(item)
        return item

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355