1

我想将下载图像的文件名从它现在得到的哈希值更改为图像 alt 标签或类似的东西。

from scrapy.http import Request
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem
from scrapy.http import Request

class DocosPipeline(object):
    def process_item(self, item, spider):
        return item

class DocosImagesPipeline(ImagesPipeline):

def get_media_requests(self, item, info):
    for image_url in item['image_urls']:
        yield Request(image_url)

def item_completed(self, results, item, info):
    image_paths = [x['path'] for ok, x in results if ok]
    if not image_paths:
        raise DropItem("Item contains no images")
    item['image_paths'] = image_paths
    return item

我尝试过覆盖 image_key 类,但似乎无法正确处理。这是课程:

def image_key(self, url):
    image_guid = hashlib.sha1(url).hexdigest()
    return 'full/%s.jpg' % (image_guid)

我真的被困在这里,任何帮助将不胜感激。

4

1 回答 1

0

我不确定您将 image_key 类放在哪里,但是下面的代码对我来说很好用

class MyImagesPipeline(ImagesPipeline):

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

    def get_media_requests(self, item, info):
于 2013-01-25T05:43:30.553 回答