抓图片下载如何使用自定义文件名

|| 对于我的项目,我目前正在使用ImagesPipeline。下载的图像以其URL的SHA1哈希作为文件名存储。 如何使用我自己的自定义文件名存储文件? 如果我的自定义文件名需要包含同一项目中的另一个抓取字段,该怎么办?例如使用
item[\'desc\']
和带有with1ѭ的图像文件名。如果我正确理解,那将涉及以某种方式从图像管道访问其他项目字段。 任何帮助将不胜感激。     
已邀请:
        这就是我在Scrapy 0.10中解决问题的方式。 检查FSImagesStoreChangeableDirectory的persist_image方法。下载图像的文件名是密钥
class FSImagesStoreChangeableDirectory(FSImagesStore):

    def persist_image(self, key, image, buf, info,append_path):

        absolute_path = self._get_filesystem_path(append_path+\'/\'+key)
        self._mkdir(os.path.dirname(absolute_path), info)
        image.save(absolute_path)

class ProjectPipeline(ImagesPipeline):

    def __init__(self):
        super(ImagesPipeline, self).__init__()
        store_uri = settings.IMAGES_STORE
        if not store_uri:
            raise NotConfigured
        self.store = FSImagesStoreChangeableDirectory(store_uri)
    
        这只是对scrapy 0.24(EDITED)答案的实现,其中不赞成
image_key()
class MyImagesPipeline(ImagesPipeline):

    #Name download version
    def file_path(self, request, response=None, info=None):
        #item=request.meta[\'item\'] # Like this you can use all from item, not just url.
        image_guid = request.url.split(\'/\')[-1]
        return \'full/%s\' % (image_guid)

    #Name thumbnail version
    def thumb_path(self, request, thumb_id, response=None, info=None):
        image_guid = thumb_id + response.url.split(\'/\')[-1]
        return \'thumbs/%s/%s.jpg\' % (thumb_id, image_guid)

    def get_media_requests(self, item, info):
        #yield Request(item[\'images\']) # Adding meta. Dunno how to put it in one line :-)
        for image in item[\'images\']:
            yield Request(image)
    
        在草率的0.12中,我解决了这样的问题
class MyImagesPipeline(ImagesPipeline):

    #Name download version
    def image_key(self, url):
        image_guid = url.split(\'/\')[-1]
        return \'full/%s.jpg\' % (image_guid)

    #Name thumbnail version
    def thumb_key(self, url, thumb_id):
        image_guid = thumb_id + url.split(\'/\')[-1]
        return \'thumbs/%s/%s.jpg\' % (thumb_id, image_guid)

    def get_media_requests(self, item, info):
        yield Request(item[\'images\'])
    
        我在2017年找到了自己的方式,抓狂的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)
就像上面的代码一样,您可以在
get_media_requests()
中将您想要的名称添加到Request meta中,然后在
file_path()
中将其取回
request.meta.get(\'yourname\',\'\')
。     
        我为此做了一个讨厌的快速hack。就我而言,我将图片的标题存储在了Feed中。而且,每个项目只有1个10英镑,因此,我写了以下脚本。基本上,它将I11ѭ目录中的图像文件重命名为我作为json存储在项目Feed中的相应标题。
import os
import json

img_dir = os.path.join(os.getcwd(), \'images\\\\full\')
item_dir = os.path.join(os.getcwd(), \'data.json\')

with open(item_dir, \'r\') as item_json:
    items = json.load(item_json)

for item in items:
    if len(item[\'images\']) > 0:
        cur_file = item[\'images\'][0][\'path\'].split(\'/\')[-1]
        cur_format = cur_file.split(\'.\')[-1]
        new_title = item[\'title\']+\'.%s\'%cur_format
        file_path = os.path.join(img_dir, cur_file)
        os.rename(file_path, os.path.join(img_dir, new_title))
很讨厌,不建议使用。但是,这是一种幼稚的替代方法。     
        我重写了代码,在thumb_path def中将\“ response。\”更改为\“ request。\”。如果否,则因为\“响应设置为无\”而无法使用。
class MyImagesPipeline(ImagesPipeline):

    #Name download version
    def file_path(self, request, response=None, info=None):
        #item=request.meta[\'item\'] # Like this you can use all from item, not just url.
        image_guid = request.url.split(\'/\')[-1]
        return \'full/%s\' % (image_guid)

    #Name thumbnail version
    def thumb_path(self, request, thumb_id, response=None, info=None):
        image_guid = thumb_id + request.url.split(\'/\')[-1]
        return \'thumbs/%s/%s.jpg\' % (thumb_id, image_guid)

    def get_media_requests(self, item, info):
        #yield Request(item[\'images\']) # Adding meta. Dunno how to put it in one line :-)
        for image in item[\'images\']:
            yield Request(image)
    

要回复问题请先登录注册