Scrapy image download how to use custom filename

前端 未结 6 1249
眼角桃花
眼角桃花 2020-11-28 07:27

For my scrapy project I\'m currently using the ImagesPipeline. The downloaded images are stored with a SHA1 hash of their URLs as the file names.

How can I s

6条回答
  •  遥遥无期
    2020-11-28 07:59

    I found my way in 2017,scrapy 1.1.3

    def file_path(self, request, response=None, info=None):
        return request.meta.get('filename','')
    
    def get_media_requests(self, item, info):
        img_url = item['img_url']
        meta = {'filename': item['name']}
        yield Request(url=img_url, meta=meta)
    

    like the code above,you can add the name you want to a Request meta in get_media_requests(), and get it back in file_path() by request.meta.get('yourname','').

提交回复
热议问题