Scrapy 第一次爬虫
生活随笔
收集整理的這篇文章主要介紹了
Scrapy 第一次爬虫
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
抓取某游戲網(wǎng)站的英雄技能數(shù)據(jù)
(1)嘗試抓取技能名稱:
import scrapy
import loggingclass 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)
輸出結(jié)果:2019-06-26 15:49:36 [root] INFO: ['懲擊', '終結(jié)', '狂暴', '疾跑', '治療術(shù)', '干擾', '暈眩', '凈化', '弱化', '閃現(xiàn)']
2019-06-26 15:49:36 [root] INFO: KPL 技能名稱 懲擊
2019-06-26 15:49:36 [root] INFO: KPL 技能名稱 終結(jié)
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 技能名稱 治療術(shù)
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 技能名稱 閃現(xiàn)
(2)抓取技能對(duì)應(yīng)的圖標(biāo)鏈接及對(duì)應(yīng)ID
def parse(self, response):spellList = response.css("#spellList li")for spell in spellList:#標(biāo)簽名::attr(屬性名) 獲取某一標(biāo)簽下的屬性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) 輸出結(jié)果: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 終結(jié) 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 治療術(shù) 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 閃現(xiàn)(3)把數(shù)據(jù)寫進(jìn)個(gè)文件里面
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' # 爬取的內(nèi)容存入文件,文件名為:作者-語錄.txtf = open(fileName, "a+") # 追加寫入文件f.write("id:"+id ) # 寫入ID內(nèi)容f.write('\n') # 換行f.write("name:"+name) # 寫入技能名字內(nèi)容f.write('\n') # 換行f.write("img:https:"+ img) # 寫入圖片鏈接內(nèi)容f.write('\n') # 換行f.close() # 關(guān)閉文件操作?
總結(jié)
以上是生活随笔為你收集整理的Scrapy 第一次爬虫的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#大文件上传支持切片上传
- 下一篇: Conflux 的自我进化:从 DAG