根据Request for Comment (RFC) 6266Content-Disposition Header Field:
不是 HTTP 标准的一部分,但由于它被广泛实施,
我们正在为实施者记录它的使用和风险。
由于 Content-Disposition 标头并不总是可用,您可以使用一种解决方案,不仅查找该特定标头,还查看 Content-Type 标头中的各个文件类型
这是Content-Types的列表。
下面的代码检查 Content-Disposition 的标头,但它也检查一些通常可下载的 Content-Type 的标头。
我还添加了对 Content-Length 的检查, 因为它可能有助于对正在下载的文件进行分块。
您是否考虑过创建子下载文件夹?
- download_folder/text_files
- download_folder/pdf_files
或
- download_folder/01242021/text_files
- download_folder/01242021/pdf_files
import requests
urls = ['https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2019-financial'
'-year-provisional/Download-data/annual-enterprise-survey-2019-financial-year-provisional-csv.csv',
'http://www.pdf995.com/samples/pdf.pdf', 'https://jeroen.github.io/files/sample.rtf',
'https://www.cnn.com/2021/01/23/opinions/biden-climate-change-gillette-wyoming-coal-sutter/index.html',
'https://www.google.com',
'https://thumbs-prod.si-cdn.com/d4e3zqOM5KUq8m0m-AFVxuqa5ZM=/800x600/filters:no_upscale():focal(554x699:555x700)/https://public-media.si-cdn.com/filer/a4/04/a404c799-7118-459a-8de4-89e4a44b124f/img_1317.jpg',
'https://www.blank.org']
for url in urls:
headers = requests.head(url).headers
Content_Length = [value for key, value in headers.items() if key == 'Content-Length']
if len(Content_Length) > 0:
Content_Size = ''.join(map(str, Content_Length))
else:
Content_Size = 'The content size was not available.'
Content_Disposition_Exists = bool({key: value for key, value in headers.items() if key == 'Content_Disposition'})
if Content_Disposition_Exists is True:
# do something with the file
pass
else:
Content_Type = {value for key, value in headers.items() if key == 'Content-Type'}
compression_formats = ['application/gzip', 'application/vnd.rar', 'application/x-7z-compressed',
'application/zip', 'application/x-tar']
compressed_file = bool([file_format for file_format in compression_formats if file_format in Content_Type])
image_formats = ['image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', 'image/tiff',
'image/webp']
image_file = bool([file_format for file_format in image_formats if file_format in Content_Type])
text_formats = ['application/rtf', 'text/plain']
text_file = bool([file_format for file_format in text_formats if file_format in Content_Type])
if compressed_file is True:
print('Compressed file')
print(Content_Size)
elif image_file is True:
print('Image file')
print(Content_Size)
elif text_file is True:
print('Text file')
print(Content_Size)
elif 'application/pdf' in Content_Type:
print('PDF file')
print(Content_Size)
elif 'text/csv' in Content_Type:
print('CSV File')
print(Content_Size)
这是另一个带有函数的版本
import requests
urls = ['https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2019-financial'
'-year-provisional/Download-data/annual-enterprise-survey-2019-financial-year-provisional-csv.csv',
'http://www.pdf995.com/samples/pdf.pdf', 'https://jeroen.github.io/files/sample.rtf',
'https://www.cnn.com/2021/01/23/opinions/biden-climate-change-gillette-wyoming-coal-sutter/index.html',
'https://www.google.com',
'https://thumbs-prod.si-cdn.com/d4e3zqOM5KUq8m0m-AFVxuqa5ZM=/800x600/filters:no_upscale():focal(554x699:555x700)/https://public-media.si-cdn.com/filer/a4/04/a404c799-7118-459a-8de4-89e4a44b124f/img_1317.jpg',
'https://www.blank.org']
def query_headers(webpage):
response = requests.get(webpage, stream=True)
headers = response.headers
file_name = webpage.rsplit('/', 1)[-1]
Content_Disposition_Exists = bool({key: value for key, value in headers.items() if key == 'Content_Disposition'})
if Content_Disposition_Exists is True:
# do something with the file
pass
else:
Content_Type = {value for key, value in headers.items() if key == 'Content-Type'}
compression_formats = ['application/gzip', 'application/vnd.rar', 'application/x-7z-compressed',
'application/zip', 'application/x-tar']
compressed_file = bool([file_format for file_format in compression_formats if file_format in Content_Type])
image_formats = ['image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', 'image/tiff',
'image/webp']
image_file = bool([file_format for file_format in image_formats if file_format in Content_Type])
text_formats = ['application/rtf', 'text/plain']
text_file = bool([file_format for file_format in text_formats if file_format in Content_Type])
nl = '\n'
if compressed_file is True:
download_file(file_name, response)
content_size = get_content_size(headers)
return f'File Information: file_type: Compressed file, File size: {content_size}, File name: {file_name}'
elif image_file is True:
download_file(file_name, response)
content_size = get_content_size(headers)
return f'File Information: file_type: Image file, File size: {content_size}, File name: {file_name}'
elif text_file is True:
download_file(file_name, response)
content_size = get_content_size(headers)
return f'File Information: file_type: Text file, File size: {content_size}, File name: {file_name}'
elif 'application/pdf' in Content_Type:
download_file(file_name, response)
content_size = get_content_size(headers)
return f'File Information: file_type: PDF file, File size: {content_size}, File name: {file_name}'
elif 'text/csv' in Content_Type:
download_file(file_name, response)
content_size = get_content_size(headers)
return f'File Information: file_type: CSV file, File size: {content_size}, File name: {file_name}'
elif 'text/html' in "".join(str(Content_Type)):
download_file(file_name, response)
content_size = get_content_size(headers)
return f'File Information: file_type: HTML file, File size: {content_size}, File name: {file_name}'
else:
content_size = get_content_size(headers)
return f'File Information: file_type: no file type found, File size: {content_size}, File name: {file_name}'
def get_content_size(headers):
Content_Length = [value for key, value in headers.items() if key == 'Content-Length']
if len(Content_Length) > 0:
Content_Size = ''.join(map(str, Content_Length))
return int(Content_Size)
else:
return 0
def download_file(filename, file_stream):
with open(f'{filename}', 'wb') as f:
f.write(file_stream.content)
for url in urls:
download_info = query_headers(url)
print(download_info)
# output
File Information: file_type: CSV file, File size: 253178, File name: annual-enterprise-survey-2019-financial-year-provisional-csv.csv
File Information: file_type: PDF file, File size: 433994, File name: pdf.pdf
File Information: file_type: Text file, File size: 9636, File name: sample.rtf
File Information: file_type: HTML file, File size: 185243, File name: index.html
File Information: file_type: HTML file, File size: 0, File name: www.google.com
File Information: file_type: Image file, File size: 78868, File name: img_1317.jpg
File Information: file_type: HTML file, File size: 170, File name: www.blank.org