抓取某游戏网站的英雄技能数据
(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() # 关闭文件操作