Scrapy框架-数据持久化存储
scrapy的高性能持久化存储操作
- 基于终端指令的持久化存储
- 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作。
-
import scrapy class QiubaiSpider(scrapy.Spider): name = 'qiubai' allowed_domains = ['https://www.qiushibaike.com/'] start_urls = ['https://www.qiushibaike.com/'] def parse(self, response): #xpath为response中的方法,可以将xpath表达式直接作用于该函数中 odiv = response.xpath('//div[@id="content-left"]/div') content_list = [] #用于存储解析到的数据 for div in odiv: #xpath函数返回的为列表,列表中存放的数据为Selector类型的数据。我们解析到的内容被封装在了Selector对象中,需要调用extract()函数将解析的内容从Selecor中取出。 author = div.xpath('.//div[@class="author clearfix"]/a/h2/text()')[0].extract() content=div.xpath('.//div[@class="content"]/span/text()')[0].extract() #将解析到的内容封装到字典中 dic={ '作者':author, '内容':content } #将数据存储到content_list这个列表中 content_list.append(dic) return content_list
- 执行指令:
- 执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
scrapy crawl 爬虫名称 -o xxx.json
scrapy crawl 爬虫名称 -o xxx.xml
scrapy crawl 爬虫名称 -o xxx.csv
- 执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
-
基于管道的持久化存储操作
- scrapy框架中已经为我们专门集成好了高效、便捷的持久化操作功能,我们直接使用即可。要想使用scrapy的持久化操作功能,我们首先来认识如下两个文件:
- items.py:数据结构模板文件。定义数据属性。
-
pipelines.py:管道文件。接收数据(items),进行持久化操作。
-
持久化流程:
- 1.爬虫文件爬取到数据后,需要将数据封装到items对象中。
- 2.使用yield关键字将items对象提交给pipelines管道进行持久化操作。
- 3.在管道文件中的process_item方法中接收爬虫文件提交过来的item对象,然后编写持久化存储的代码将item对象中存储的数据进行持久化存储
- 4.settings.py配置文件中开启管道
-
小试牛刀:将糗事百科首页中的段子和作者数据爬取下来,然后进行持久化存储
-
-
爬虫文件:qiubaiDemo.py import scrapy from secondblood.items import SecondbloodItem class QiubaidemoSpider(scrapy.Spider): name = 'qiubaiDemo' allowed_domains = ['www.qiushibaike.com'] start_urls = ['http://www.qiushibaike.com/'] def parse(self, response): odiv = response.xpath('//div[@id="content-left"]/div') for div in odiv: # xpath函数返回的为列表,列表中存放的数据为Selector类型的数据。我们解析到的内容被封装在了Selector对象中,需要调用extract()函数将解析的内容从Selecor中取出。 author = div.xpath('.//div[@class="author clearfix"]//h2/text()').extract_first() author = author.strip('\n')#过滤空行 content = div.xpath('.//div[@class="content"]/span/text()').extract_first() content = content.strip('\n')#过滤空行 #将解析到的数据封装至items对象中 item = SecondbloodItem() item['author'] = author item['content'] = content
yield item#提交item到管道文件(pipelines.py)
items文件:items.py import scrapy class SecondbloodItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() author = scrapy.Field() #存储作者 content = scrapy.Field() #存储段子内容
-
-
管道文件:pipelines.py
-
class SecondbloodPipeline(object): fp = None #定义一个文件描述符属性 #下列都是在重写父类的方法: #开始爬虫时,执行一次 def open_spider(self,spider): print('爬虫开始') self.fp = open('./data.txt', 'w') #因为该方法会被执行调用多次,所以文件的开启和关闭操作写在了另外两个只会各自执行一次的方法中。 def process_item(self, item, spider): #将爬虫程序提交的item进行持久化存储 self.fp.write(item['author'] + ':' + item['content'] + '\n') return item #结束爬虫时,执行一次 def close_spider(self,spider): self.fp.close() print('爬虫结束')
其他数据库存储类型
-
-
import pymysql from redis import Redis
#存储在本地文件中 class BossproPipeline(object): fp = None def open_spider(self, spider): print('开始爬虫......') self.fp = open('./boss.txt','w',encoding='utf-8') def close_spider(self, spider): print('结束爬虫......') self.fp.close() #爬虫文件每向管道提交一次item,则该方法就会被调用一次. #参数:item 就是管道接收到的item类型对象 def process_item(self, item, spider): #print(item) self.fp.write(item['job_name']+':'+item['salary']+':'+item['company']+'\n') return item #返回给下一个即将被执行的管道类 #存储到mysql数据库中 class mysqlPileLine(object): conn = None cursor =None def open_spider(self,spider): self.conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='',db='scrapy',charset="utf8") print(self.conn) def process_item(self, item, spider): self.cursor = self.conn.cursor() # print(item) #print('insert into boss values ("%s","%s","%s")'%(item['job_name'],item['salary'],item['company'])) try: print('insert into boss values ("%s","%s","%s")'%(item['job_name'],item['salary'],item['company'])) self.cursor.execute('insert into boss values ("%s","%s","%s")'%(item['job_name'],item['salary'],item['company'])) self.conn.commit() except Exception as e: print(e) self.conn.rollback() def close_spider(self,spider): self.conn.close() self.cursor.close() #存储进redis数据库缓存中 class redisPileLine(object): conn = None def open_spider(self,spider): self.conn = Redis(host='127.0.0.1',port=6379) print(self.conn) def process_item(self, item, spider): # print(item) dic = { 'name':item['job_name'], 'salary':item['salary'], 'company':item['company'] } self.conn.lpush('boss',dic)
- 配置文件
-
#开启管道
#下列结构为字典,字典中的键值表示的是即将被启用执行的管道文件和其执行的优先级。ITEM_PIPELINES = {
#'bossPro.pipelines.BossproPipeline': 300,
'bossPro.pipelines.mysqlPileLine': 301,
#'bossPro.pipelines.redisPileLine': 302,
}#上述代码中,字典中的两组键值分别表示会执行管道文件中对应的两个管道类中的process_item方法,实现两种不同形式的持久化操作。
-
-
- 面试题:如果最终需要将爬取到的数据值一份存储到磁盘文件,一份存储到数据库中,则应该如何操作scrapy?
-
答:管道文件中的代码为
-
#该类为管道类,该类中的process_item方法是用来实现持久化存储操作的。 class DoublekillPipeline(object): def process_item(self, item, spider): #持久化操作代码 (方式1:写入磁盘文件) return item #如果想实现另一种形式的持久化操作,则可以再定制一个管道类: class DoublekillPipeline_db(object): def process_item(self, item, spider): #持久化操作代码 (方式1:写入数据库) return item
-

浙公网安备 33010602011771号