Scrapy图片自动下载配置

来源:互联网 发布:2017淘宝店铺首页装修 编辑:程序博客网 时间:2024/05/21 18:32

在setting.py中配置基本信息

IMAGES_URLS_FIELD = "front_image_url"# 获取当前文件路径project_dir = os.path.abspath(os.path.dirname(__file__))# 设置图片保存路径IMAGES_STORE = os.path.join(project_dir, 'images')

同时打开

ITEM_PIPELINES = {     # 自动下载图片配置,这个是scrapy自带的     'scrapy.pipelines.images.ArticleImagePipeline':1,}

但是很多情况下我们需要获取我们保存的图片的路径的(包括名称,这样我们就可定位到不同item对应的图片),这时候我们需要自定义下载类

进入pipelines.py中添加下面的方法

# 自定义Pipelineclass ArticleImagePipeline(ImagesPipeline):    def item_completed(self, results, item, info):        for ok, value in results:            image_file_path = value["path"]  # 获取文件路径        item["front_image_path"] = image_file_path        return item        # 可以通通过reslts获取保存路径

然后需要在setting中改动