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里這個函數就是獲取后綴的。
綜上,文件名可以在這邊修改。
但是有一個問題,如果想要下載的文件的url是經過重定向,或者對應的url沒有后綴呢。
由於網頁一般會將想要請求的文件類型放在response的頭部信息 content-type里,我們可以通過獲取content-type信息,在進行相應的操作。這樣我們就需要找到調用
由於網頁一般會將想要請求的文件類型放在response的頭部信息 content-type里,我們可以通過獲取content-type信息,在進行相應的操作。這樣我們就需要找到調用
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,直接正則獲取最后的文件路徑
是一個擴展協議,對得到的內容進行正則處理后,可以得到后綴,一般建議先用這個。但有的並不支持這種協議
一般網站都是支持的,但是它返回的文件類型可能沒法直接使用,所以建議先使用上面的那個
但是有一個問題,如果想要下載的文件的url是經過重定向,或者對應的url沒有后綴呢。
由於網頁一般會將想要請求的文件類型放在response的頭部信息 content-type里,我們可以通過獲取content-type信息,在進行相應的操作。這樣我們就需要找到調用
由於網頁一般會將想要請求的文件類型放在response的頭部信息 content-type里,我們可以通過獲取content-type信息,在進行相應的操作。這樣我們就需要找到調用
file_path的函數
作者:老鼠慎言
鏈接:https://www.jianshu.com/p/d1bb28cbb6a8
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。
