Scrapy 自带了 Feed 输出,并且支持多种序列化格式(serialization format)及存储方式(storage backends)。
Python3 的 Scrapy 爬虫框架 中数据爬取过程中数据输出操作。生成一个带有爬取数据的“输出文件(通常叫『输出 feed』),来供其它系统使用。
数据类型 | 输出格式化 | Item 类型输出 |
JSON | json | JsonItemExporter |
JSON lines | jsonlines | JsonLinesItemExporter |
CSV | csv | CsvItemExporter |
XML | xml | XmlItemExporter |
Pickle | pickle | PickleItemExporter |
Marshal | marshal | MarshalItemExporter |
在这里插入图片描述
使用 feed 输出时可以通过使用 URL(通过 FEED_URI 设置)来定义存储端。feed 输出支持 URI 方式支持的多种存储后端类型。
自带支持的存储后端有:本地文件系统、FTP、S3(需要 boto)、标注输出。
存储 URI 也包含参数。当 feed 被创建时这些参数可以被覆盖。
# 存储在 FTP,每个 spider 一个目录ftp://user:password@ftp.example.com/scraping/feeds/%(name)s/%(time)s.json# 存储在 S3,每个 spider 一个目录s3://mybucket/scraping/feeds/%(name)s/%(time)s.json
存储类型 | 系统限制 | URI scheme | 依赖库 | 样例 |
本地文件系统 | Unix | file | - | file://tmp/export.csv |
FTP | - | ftp | - | tp://user:http://pass@ftp.example.com/path/to/export.csv |
S3 | - | s3 | boto | s3://aws_key:aws_secret@mybucket/path/to/export.csv |
谷歌云存储(GCS) | - | gs | - | gs://mybucket/path/to/export.csv |
标准输出 | - | stdout | - | stdout: |
用于选择是否应允许将项目导出到特定的数据内容。
自定义过滤类别分配 item_filter ,触发条件即过滤。
class MyCustomFilter: def __init__(self, feed_options): self.feed_options = feed_options def accepts(self, item): if "xxx" in item and item["xx"] == "xxxx": return True return False
Scrapy 提供了一个选项来激活插件以在将数据导出到 feed 存储之前对它们进行后处理。
通过 postprocessing 选项激活该功能。主要功能参数有:
class scrapy.extensions.postprocessing.GzipPlugin
class scrapy.extensions.postprocessing.LZMAPlugin(file: BinaryIO, feed_options: Dict[str, Any])
class scrapy.extensions.postprocessing.Bz2Plugin(file: BinaryIO, feed_options: Dict[str, Any])
- FEEDS(强制性)- FEED_EXPORT_ENCODING- FEED_STORE_EMPTY- FEED_EXPORT_FIELDS- FEED_EXPORT_INDENT- FEED_STORAGES- FEED_STORAGE_FTP_ACTIVE- FEED_STORAGE_S3_ACL- FEED_EXPORTERS- FEED_EXPORT_BATCH_ITEM_COUNT
默认输出字典格式。启用提要导出功能需要此设置。
{ 'items.json': { 'format': 'json', 'encoding': 'utf8', 'store_empty': False, 'fields': None, 'indent': 4, 'item_export_kwargs': { 'export_empty_fields': True, }, }, '/home/user/documents/items.xml': { 'format': 'xml', 'fields': ['name', 'price'], 'encoding': 'latin1', 'indent': 8, }, pathlib.Path('items.csv'): { 'format': 'csv', 'fields': ['price', 'name'], },}
主要参数列表:
文件存储基础字典。
{ '': 'scrapy.extensions.feedexport.FileFeedStorage', 'file': 'scrapy.extensions.feedexport.FileFeedStorage', 'stdout': 'scrapy.extensions.feedexport.StdoutFeedStorage', 's3': 'scrapy.extensions.feedexport.S3FeedStorage', 'ftp': 'scrapy.extensions.feedexport.FTPFeedStorage',}
文件输出基础字典。
{ 'json': 'scrapy.exporters.JsonItemExporter', 'jsonlines': 'scrapy.exporters.JsonLinesItemExporter', 'jl': 'scrapy.exporters.JsonLinesItemExporter', 'csv': 'scrapy.exporters.CsvItemExporter', 'xml': 'scrapy.exporters.XmlItemExporter', 'marshal': 'scrapy.exporters.MarshalItemExporter', 'pickle': 'scrapy.exporters.PickleItemExporter',}
Scrapy生成多个输出文件,存储到每个输出文件中指定的项目数。
setting.py中设置,输出信息则设置了最大值。
FEED_EXPORT_BATCH_ITEM_COUNT = 100
用于设置参数以将 printf 样式的字符串格式。
scrapy crawl -o "%(spider_name)s.jl"
基本无用,可以忽略了。
留言与评论(共有 0 条评论) “” |