自定义图片/文档下载pipeline,自定义一个自己需要的路径来存储下载的图片/文档
自定义pipeline可以基于scrapy自带的ImagesPipeline的基础上完成。
可以重写ImagesPipeline中的三个法:get_media_requests(),file_path(),item_completed()
首先是在spider.py(自己的爬虫文件)文件中获取自己想要添加路径的名字,name为自己添加的文件路径
item = ZhanzhangsucaispiderItem()
item["name"]=response.meta["name"]#meta是以字典是的形式传给response的.
item["img_url"] = [src]
yield item
然后将item返回出去,再在items.py文件中声明一下name, img_path是pipeline.py文件中需要的。后边会介绍。
class ZhanzhangsucaispiderItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
img_url = scrapy.Field()
img_path = scrapy.Field()
接下来修改pipeline.py文件
import scrapy
#导入系统文件images.py里的ImagesPipeline类.
from scr