gpt4 book ai didi

python /碎片 : Custom pipeline has no effect/download files with custom filename

转载 作者:行者123 更新时间:2023-12-04 14:21:07 25 4
gpt4 key购买 nike

这是我的 initial question 的后续问题.我想下载 PDF 并使用自定义文件名将它们保存在硬盘上。

对于自定义文件名,我根据此 recommendation 在我的 pipelines.py 中尝试了此代码:

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

def file_path(self, request, response=None, info=None):
original_path = super(PrangerPipeline, self).file_path(request, response=None, info=None)
sha1_and_extension = original_path.split('/')[1] # delete 'full/' from the path
return request.meta.get('filename','') + "_" + sha1_and_extension

def get_media_requests(self, item, info):
file_url = item['file_url']
meta = {'filename': item['name']}
yield Request(url=file_url, meta=meta)

在我的 settings.py 中我有:

ITEM_PIPELINES = {
'pranger.pipelines.PrangerPipeline': 1,
'scrapy.pipelines.files.FilesPipeline': 2,
}

但文件仅使用其 SHA1 哈希值保存,例如:a8569143c987cdd43dd1f6d9a6f98b7aa6fbc284.PDF。所以我自定义的file_path函数似乎没有被Scrapy使用。

当我注释掉该行时

'scrapy.pipelines.files.FilesPipeline': 2,

不会下载任何内容。

我很困惑...

最佳答案

您的问题是您的自定义管道不是真正的文件管道,因此它什么都不做。您需要子类原始FilesPipeline,然后在设置中仅使用PrangerPipeline

例如:

pipelines.py:

from scrapy.pipelines.files import FilesPipeline

class PrangerPipeline(FilesPipeline):

# Don't override process_item. The parent class handles it.

def file_path(self, request, response=None, info=None):
# ...

def get_media_requests(self, item, info):
# ...

settings.py:

ITEM_PIPELINES = {
'pranger.pipelines.PrangerPipeline': 1,
}

请引用我在此处使用 ImagesPipeline 的示例:

Unable to rename downloaded images through pipelines without the usage of item.py

Trouble renaming downloaded images in a customized manner through pipelines

关于 python /碎片 : Custom pipeline has no effect/download files with custom filename,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com