Scrapy 第一次爬虫

抓取某游戏网站的英雄技能数据


(1)尝试抓取技能名称:

import scrapy
import logging


class SpellList(scrapy.Spider):
    name = "SpellList"
    start_urls = [
        "https://pvp.qq.com/web201605/summoner.shtml",
    ]
    def parse(self, response):
        spellList = response.css("#spellList p::text").extract()
        logging.info(spellList)
        for spell in spellList:
            logging.info("KPL 技能名称 " + spell)
输出结果:

2019-06-26 15:49:36 [root] INFO: ['惩击', '终结', '狂暴', '疾跑', '治疗术', '干扰', '晕眩', '净化', '弱化', '闪现']
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 惩击
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 终结
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 狂暴
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 疾跑
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 治疗术
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 干扰
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 晕眩
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 净化
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 弱化
2019-06-26 15:49:36 [root] INFO: KPL 技能名称 闪现

(2)抓取技能对应的图标链接及对应ID

    def parse(self, response):
        spellList = response.css("#spellList li")
        for spell in spellList:
            #标签名::attr(属性名) 获取某一标签下的属性
            id = spell.css("li::attr(id)").extract_first() 
            img = spell.css("img::attr(src)").extract_first()
            name = spell.css("p::text").extract_first()
            logging.info("id "+id)
            logging.info("img "+img)
            logging.info("name "+name)
输出结果:

2019-06-26 16:54:04 [root] INFO: id 80104
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80104.jpg
2019-06-26 16:54:04 [root] INFO: name 惩击
2019-06-26 16:54:04 [root] INFO: id 80108
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80108.jpg
2019-06-26 16:54:04 [root] INFO: name 终结
2019-06-26 16:54:04 [root] INFO: id 80110
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80110.jpg
2019-06-26 16:54:04 [root] INFO: name 狂暴
2019-06-26 16:54:04 [root] INFO: id 80109
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80109.jpg
2019-06-26 16:54:04 [root] INFO: name 疾跑
2019-06-26 16:54:04 [root] INFO: id 80102
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80102.jpg
2019-06-26 16:54:04 [root] INFO: name 治疗术
2019-06-26 16:54:04 [root] INFO: id 80105
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80105.jpg
2019-06-26 16:54:04 [root] INFO: name 干扰
2019-06-26 16:54:04 [root] INFO: id 80103
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80103.jpg
2019-06-26 16:54:04 [root] INFO: name 晕眩
2019-06-26 16:54:04 [root] INFO: id 80107
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80107.jpg
2019-06-26 16:54:04 [root] INFO: name 净化
2019-06-26 16:54:04 [root] INFO: id 80121
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80121.jpg
2019-06-26 16:54:04 [root] INFO: name 弱化
2019-06-26 16:54:04 [root] INFO: id 80115
2019-06-26 16:54:04 [root] INFO: img //game.gtimg.cn/images/yxzj/img201606/summoner/80115.jpg
2019-06-26 16:54:04 [root] INFO: name 闪现

(3)把数据写进个文件里面

 def parse(self, response):
        spellList = response.css("#spellList li")
        for spell in spellList:
            id = spell.css("li::attr(id)").extract_first()
            img = spell.css("img::attr(src)").extract_first()
            name = spell.css("p::text").extract_first()

            fileName = '技能.txt'  # 爬取的内容存入文件,文件名为:作者-语录.txt
            f = open(fileName, "a+")  # 追加写入文件
            f.write("id:"+id )  # 写入ID内容
            f.write('\n')  # 换行
            f.write("name:"+name)  # 写入技能名字内容
            f.write('\n')  # 换行
            f.write("img:https:"+ img)  # 写入图片链接内容
            f.write('\n')  # 换行
            f.close()  # 关闭文件操作

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值