【发布时间】:2020-08-25 23:44:40
【问题描述】:
我正在为一家艺术博物馆开发抓取程序。
我是 Scrapy 框架的新手,充其量是 python 中级
我需要从网站下载图像并使用解析数据的值相应地命名它们。
我一直在浏览 Scrapy 文档和 Google 搜索,但到目前为止还没有运气。我被困在管道上。
我知道如何在使用包装程序运行 Scrapy 后修复文件名,但这似乎适得其反且草率。
蜘蛛产生的每个项目如下所示:
{'Artist': 'SomeArtist',
...
'Image Url': 'https://www.nationalgallery.org.uk/media/33219/n-1171-00-000049-hd.jpg',
'Inventory number': 'NG1171'}
我需要给图片命名'Inventory number'
我设法制作了一个自定义管道,但没有让它按我想要的方式工作。
我得到的最接近的是这个,但它通过为许多图像分配相同的self.file_name 值而惨遭失败
class DownloadPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
# The only point, that I've found, for accessing item dict before downloading
self.file_name = item['Inventory number']
yield Request(item["Image Url"])
def file_path(self, request, response=None, info=None):
return f"Images/{self.file_name}.jpg"
这样的东西会很棒:
class DownloadPipeline(ImagesPipeline):
def file_path(self, request, item, response=None, info=None):
file_name = item['Inventory number']
return f"Images/{file_name}.jpg"
有什么办法可以做到吗?
【问题讨论】: