我使用 python 的 scrapy 模块创建了一个脚本,从 torrent 站点下载并重命名电影图像,并将它们存储在 scrapy 项目内的文件夹中。当我按原样运行脚本时,我发现它正确地下载了该文件夹中的图像。
此时,脚本正在使用 request.url 中的方便部分重命名这些图像pipelines.py
.
如何通过以下方式重命名下载的图像pipelines.py
覆盖get_media_requests()
并将您需要的数据添加到请求中。然后从请求中获取该数据file_path()
.
例如:
class YifySpiderPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
# Here we add the whole item, but you can add only a single field too.
return [Request(x, meta={'item': item) for x in item.get(self.images_urls_field, [])]
def file_path(self, request, response=None, info=None):
item = request.meta.get('item')
movie = item['movie']
# Construct the filename.
return image_name
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)