scrapy框架之持久化操作

  • 基于终端指令的持久化存储
  • 基于管道的持久化存储

1 基于终端指令的持久化存储

  • 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作。
执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
scrapy crawl 爬虫名称 -o xxx.json
scrapy crawl 爬虫名称 -o xxx.xml
scrapy crawl 爬虫名称 -o xxx.csv

以爬取糗事百科(https://www.qiushibaike.com/text/)为例

import scrapy

class QiubaiSpider(scrapy.Spider):
name = 'qiubai' # 表示该爬虫文件的名称
allowed_domains = ['www.qiushibaike.com/text/']
start_urls = ['https://www.qiushibaike.com/text/']
  
  # 解析函数
def parse(self, response): # response就是对起始url发起请求后,对应的响应对象
author_list = response.xpath('//div[@id="content-left"]/div') all_data = []
for div in author_list:
       # extract_first()可以将xpath返回列表中第一个列表元素进行extract解析操作
author = div.xpath('./div/a[2]/h2/text()').extract_first()
       # extract()可以将Selector对象中存储的数据进行解析操作
        author = div.xpath('./div/a[2]/h2/text()').extract()
content = div.xpath('./a/div/span/text()').extract_first() dict={
'author':author,
'content':content
}
all_data.append(dict)
return all_data # 可迭代的对象

在终端写入

执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
scrapy crawl 爬虫名称 -o xxx.json
scrapy crawl 爬虫名称 -o xxx.xml
scrapy crawl 爬虫名称 -o xxx.csv

2 基于管道的持久化存储

scrapy框架中已经为我们专门集成好了高效、便捷的持久化操作功能,我们直接使用即可。要想使用scrapy的持久化操作功能,我们首先来认识如下两个文件:

    items.py:数据结构模板文件。定义数据属性。
pipelines.py:管道文件。接收数据(items),进行持久化操作。 持久化流程:
1.爬虫文件爬取到数据后,需要将数据封装到items对象中。
2.使用yield关键字将items对象提交给pipelines管道进行持久化操作。
3.在管道文件中的process_item方法中接收爬虫文件提交过来的item对象,然后编写持久化存储的代码将item对象中存储的数据进行持久化存储
4.settings.py配置文件中开启管道

1 爬虫文件qiubai.py

# -*- coding: utf- -*-
import scrapy
from ..items import FirstProjectItem
'''基于管道存储''' '''
爬虫文件中解析数据
【items.py】将解析到的数据值全部分装在item对象中
pipelines.py
settings.py配置文件 '''
class QiubaiSpider(scrapy.Spider):
name = 'qiubai'
allowed_domains = ['www.qiushibaike.com/text/']
start_urls = ['https://www.qiushibaike.com/text/'] def parse(self, response): author_list = response.xpath('//div[@id="content-left"]/div')      for div in author_list: author = div.xpath('./div/a[2]/h2/text()').extract_first()
# author = div.xpath('./div/a[2]/h2/text()')[].extract()
content = div.xpath('./a/div/span/text()').extract_first()
        ----------------------------------------------------
items = FirstProjectItem() items["author"] = author 重点
items["content"] = content
# 提交给管道
yield items
        ----------------------------------------------------

2 items.py

import scrapy

# items会实例化一个items对象; 用来存储解析到的数据值

class FirstProjectItem(scrapy.Item):
# define the fields for your item here like:
   -----------------------------------------
author = scrapy.Field()
content = scrapy.Field() 重点 你在第一步中有几个要持久化的这就写上对应的
-----------------------------------------

3 pipelines.py

# 爬虫文件每向管道提交一次item则process_item方法就会被执行一次
class FirstProjectPipeline(object):
                # item就是爬虫文件提交过来的
def process_item(self, item, spider):
return item

4 settings.py

# 第67行
ITEM_PIPELINES = {
'first_project.pipelines.FirstProjectPipeline': ,
}

依据上面四步我们就学会了基本的“基于管道的持久化”的步骤,但是我们要在piplines.py做一些操作

只是修改第3步pipelines.py

# -*- coding: utf- -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html class FirstProjectPipeline(object):
# 每次都会打开多次文件,我们重写 open_spider方法来开文件一次
fp = None
def open_spider(self, spider):
print('开始爬虫')
self.fp = open('qiubai1.txt', 'w', encoding='utf-8') def process_item(self, item, spider): self.fp.write(item['author']+':'+item["content"]+"\n") # 生成qiubai1.txt文件
return item def close_spider(self,spider):
print('结束爬虫')
self.fp.close()

3 写入数据库

import pymysql
class MysqlPipline(object):
cursor = None
conn = None
def open_spider(self, spider):
print('mysql开始')
self.conn = pymysql.connect(host='127.0.0.1', user='root', password='123456', port=3306, db='s18',charset='utf8')
def process_item(self, item, spider):
sql = "insert into t_qiubai VALUES ('%s','%s')"%(item["author"], item["content"])
self.cursor = self.conn.cursor()
try:
self.cursor.execute(sql)
self.conn.commit()
except Exception as e:
self.conn.rollback()
return item def close_spider(self, spider):
print('mysql结束')
self.cursor.close()
self.conn.close()

settings.py

ITEM_PIPELINES = {
'first_project.pipelines.FirstProjectPipeline': ,
'first_project.pipelines.MysqlPipline': , # settings 配置 值越小 越优先
}

4 写入redis数据库

wins安装redis

import redis

class RedisPipline(object):

    r = None

    def open_spider(self, spider):
print('redis开始')
self.r = redis.Redis(host='127.0.0.1', port=6379) def process_item(self, item, spider):
dict = {
'author':item['author'],
'content':item['content']
}
self.r.lpush('data', dict)
return item def close_spider(self, spider):
print('redis结束')

settings.py设置

ITEM_PIPELINES = {
'first_project.pipelines.FirstProjectPipeline': 300,
'first_project.pipelines.RedisPipline': 500,
}

我们可以去redis里面查看

key *   # 查看所有的key
lrange key 0 -1 # 从头到尾查看key

(六--二)scrapy框架之持久化操作的更多相关文章

  1. scrapy框架之持久化操作

    1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存储: ...

  2. 爬虫开发8.scrapy框架之持久化操作

    今日概要 基于终端指令的持久化存储 基于管道的持久化存储 今日详情 1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的 ...

  3. scrapy框架之分布式操作

    分布式概念 分布式爬虫: 1.概念:多台机器上可以执行同一个爬虫程序,实现网站数据的分布爬取. 2.原生的scrapy是不可以实现分布式爬虫? a)调度器无法共享 b)管道无法共享 3.scrapy- ...

  4. 6 scrapy框架之分布式操作

    分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux:   redis-server redis.conf windows: redis-server.exe redis-wi ...

  5. scrapy框架的持久化存储

    一 . 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存 ...

  6. 爬虫开发14.scrapy框架之分布式操作

    分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux:   redis-server redis.conf windows: redis-server.exe redis-wi ...

  7. scrapy框架之CrawlSpider操作

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  8. 爬虫开发11.scrapy框架之CrawlSpider操作

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  9. Scrapy 框架,持久化文件相关

    持久化相关 相关文件 items.py 数据结构模板文件.定义数据属性. pipelines.py 管道文件.接收数据(items),进行持久化操作. 持久化流程 1.爬虫文件爬取到数据后,需要将数据 ...

随机推荐

  1. 暴强贴:从.NET平台调用Win32 API----转载

      水之真谛 关注 17人评论 27649人阅读 2007-02-28 17:03:47 作者:刘铁猛日期:2005-12-20关键字:C# .NET Win32 API 版权声明:本文章受知识产权法 ...

  2. 使用redis集群中遇到的错误

    一. 上述错误的原因: 1.在redis服务器上关闭防火墙 2.可能是host写错了 上述错误的原因: 配置文件中jedisClient代表的是单机版的redis,但是在类中转化的时候转化的是集群版

  3. IOS 3种内省方法

    IOS提供了3种内省方法 isKindOfClass 检查当前实例是否为某类及其子类 UIView *b = [UIView new]; //... id a = b; if ([a isMember ...

  4. Linux学习计划(一)

    一.用途:网络服务器 二.优点: 1.开源免费 2.良好的可移植性 3.安全性 三.安装Linux 工具:VMware workstation .centOS7 安装步骤 图片加载中... 说明: Ⅰ ...

  5. js取值问题----key为数字

    今天远程调用一个接口在处理返回的数据的时候突然发现数组的Key是一个数字 然后如果继续用“.”的话是不会的,会报错 要用中括号就可以解决

  6. P1078 字符串压缩与解压

    P1078 字符串压缩与解压 转跳点:

  7. VUE - 引入 npm 安装的模块 以及 uuid模块的使用

    <template>   <div>       <form @submit.prevent="addTodo">         <in ...

  8. JS写一个漂亮的音乐播放器

    先放上效果图: 正如图中所展示的播放器那样,我们用HTML+CSS+JS将这个效果实现出来. HTML页面布局 <div class="music"> <div ...

  9. JavaScript的调用

    1 方法调用模式 var myObject = { value : 0, increment : function(inc) { alert('hi'); } }; myObject.incremen ...

  10. windows搭建安装react-native环境

    在win10环境下,利用Genymotion模拟器,配置react-native的环境. 一.安装JDK 在网上下载jdk,版本最好是1.8以上.安装后要对环境变量进行配置. 同时在 Path 中配置 ...