scrapy框架内置了图片处理管道,用来对响应中的图片进行下载,可以直接使用,也可继承后自定义.
1 直接使用
在settings.py文件中加入:
ITEM_PIPELINES = { # 图片处理管道 # 'scrapy.pipelines.p_w_picpaths.ImagesPipeline' : 2 } IMAGES_URLS_FIELD = 'p_w_picpath_url' # 指定从哪个字段提取图片链接 base_dir = os.path.dirname(os.path.dirname(__file__)) IMAGES_STORE = os.path.join(base_dir,'p_w_picpaths') # 设置图片存放位置
2 继承后自定义
from scrapy.pipelines.p_w_picpaths import ImagesPipeline #导入 # 保存图片的Pipeline class CnblogImagePipeline(ImagesPipeline): # def process_item(self, item, spider): # pass def item_completed(self, results, item, info): # 图片处理结果 status = results[0][0] if status: item['p_w_picpath_path'] = results[0][1]['path'] else: item['p_w_picpath_path'] = '' return item
在settin.py文件中加入该管道