标签:The none %s 加密 start cat files app col
https://www.jianshu.com/p/d1bb28cbb6a8
scrapy中负责下载文件的是class MyFilesPipeline(FilesPipeline)
类
def file_path(self, request, response=None, info=None): ## start of deprecation warning block (can be removed in the future) def _warn(): from scrapy.exceptions import ScrapyDeprecationWarning import warnings warnings.warn(‘FilesPipeline.file_key(url) method is deprecated, please use ‘ ‘file_path(request, response=None, info=None) instead‘, category=ScrapyDeprecationWarning, stacklevel=1) # check if called from file_key with url as first argument if not isinstance(request, Request): _warn() url = request else: url = request.url # detect if file_key() method has been overridden if not hasattr(self.file_key, ‘_base‘): _warn() return self.file_key(url) ## end of deprecation warning block media_guid = hashlib.sha1(to_bytes(url)).hexdigest() # change to request.url after deprecation media_ext = os.path.splitext(url)[1] # change to request.url after deprecation return ‘full/%s%s‘ % (media_guid, media_ext)
我们可以很清楚地看到 因为是下载的是文件,所以默认的response参数是为None的,因为一般来讲,文件的response就是我们下载的。我们待会说明response为None的坏处。
所以,修改文件名的办法就很显然了。在return处做文章:return 的文件路径,用了两个变量media_guid
和media_ext
media_guid
是一个将url进行哈希加密的文件,可以修改。media_ext
是什么呢: os.path.splitext(url)[1]
这个函数是将url作切割,不同于py里的split
函数,这个函数只返回两部分,示例如下import os path_01=‘D:/User/wgy/workplace/data/notMNIST_large.tar.gar‘ path_02=‘D:/User/wgy/workplace/data/notMNIST_large‘ root_01=os.path.splitext(path_01) root_02=os.path.splitext(path_02) print(root_01) print(root_02)
结果: (‘D:/User/wgy/workplace/data/notMNIST_large.tar‘, ‘.gar‘) (‘D:/User/wgy/workplace/data/notMNIST_large‘, ‘‘)
可以看到,就是在scrapy里这个函数就是获取后缀的。
综上,文件名可以在这边修改。
file_path
的函数def file_downloaded(self, response, request, info): path = self.file_path(request, response=response, info=info) buf = BytesIO(response.body) checksum = md5sum(buf) buf.seek(0) self.store.persist_file(path, buf, info) return checksum
file_downloaded
里,第一行就是调用了file_path
函数,而且根据命名规则,十分清晰。 我们只要对上述path 做一定的修改即可。file_downloaded
是对文件进行下载,而file_path
是对文件进行存储路径的安排的,所以file_downloaded
这里的response我们是可以获取相关信息的。response.headers.get(‘Content-Disposition‘)
或者 response.headers.get(‘Content-Type‘)
,如果获取不到,可以改成content-disposition
或者 content-type
,举个例子content-disposition
可能得到的是这个:Content-Disposition: inline;filename=Vet%20Contract%20for%20Services.pdf
,直接正则获取最后的文件路径file_path
的函数关于scrapy下载文件重命名的办法以及对应url没有文件后缀的办法
标签:The none %s 加密 start cat files app col
原文地址:https://www.cnblogs.com/pythonClub/p/9858830.html