2024-08-29 16:37:09 +08:00
|
|
|
import requests
|
|
|
|
import mimetypes
|
|
|
|
|
2024-09-11 12:02:09 +08:00
|
|
|
|
2024-08-29 16:37:09 +08:00
|
|
|
|
|
|
|
def download_file(url, local_filename):
|
|
|
|
try:
|
|
|
|
with requests.get(url, stream=True) as response:
|
|
|
|
response.raise_for_status() # 确保请求成功,否则抛出异常
|
|
|
|
|
|
|
|
# 获取文件类型并设置适当的文件扩展名
|
|
|
|
content_type = response.headers.get('Content-Type')
|
|
|
|
extension = mimetypes.guess_extension(content_type, strict=False)
|
|
|
|
if not extension:
|
|
|
|
# 如果无法猜测扩展名,默认使用 .docx
|
|
|
|
extension = '.docx'
|
|
|
|
full_filename = local_filename + extension # 追加扩展名
|
|
|
|
|
|
|
|
with open(full_filename, 'wb') as file:
|
|
|
|
for chunk in response.iter_content(chunk_size=8192):
|
|
|
|
file.write(chunk)
|
|
|
|
|
|
|
|
# 根据扩展名返回对应的值
|
|
|
|
if extension == '.docx':
|
|
|
|
return full_filename,1
|
|
|
|
elif extension == '.pdf':
|
|
|
|
return full_filename,2
|
|
|
|
else:
|
|
|
|
return full_filename,3
|
|
|
|
except requests.HTTPError as e:
|
2024-09-13 15:03:55 +08:00
|
|
|
print(f"download: HTTP Error: {e}")
|
2024-08-29 16:37:09 +08:00
|
|
|
return None
|
|
|
|
except requests.RequestException as e:
|
2024-09-13 15:03:55 +08:00
|
|
|
print(f"download: Error downloading the file: {e}")
|
2024-08-29 16:37:09 +08:00
|
|
|
return None
|
|
|
|
except Exception as e:
|
2024-09-13 15:03:55 +08:00
|
|
|
print(f"download: An error occurred: {e}")
|
2024-08-29 16:37:09 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# 测试下载的URL
|
2024-09-18 11:57:17 +08:00
|
|
|
test_url ="https://bid-assistance.oss-cn-wuhan-lr.aliyuncs.com/test/01%20%E6%8B%9B%E6%A0%87%E6%96%87%E4%BB%B6-%E5%A4%A7%E8%8D%94%E5%8E%BF%E5%85%AC%E5%AE%89%E5%B1%80%E6%83%85%E6%8C%87%E5%8B%A4%E8%88%86%E4%B8%80%E4%BD%93%E5%8C%96%E5%B9%B3%E5%8F%B0%E5%BB%BA%E8%AE%BE%E9%A1%B9%E7%9B%AE%28%E4%BA%8C%E6%AC%A1%29.pdf?Expires=1726661197&OSSAccessKeyId=TMP.3KdMWkHL1nqHypaY6LmhnzqJTRkTzuYLNNPhW9KZruLGLaM3YL7F45NfuF3JT4CszSe2FD7ZH6WZFUTumokmJsSEW6pPh6&Signature=7BIGVSb9YGYLKFBXeTQZm7QnTI8%3D"
|
2024-08-29 16:37:09 +08:00
|
|
|
local_file_name = 'C:\\Users\\Administrator\\Desktop\\招标文件\\output\\downloaded_file'
|
|
|
|
file_path = download_file(test_url, local_file_name)
|
|
|
|
if file_path:
|
|
|
|
print(f"Downloaded file path: {file_path}")
|