在自动化下载过程中,处理文件重命名可以确保下载的文件具有清晰、有意义且不冲突的名称,方便后续管理和使用。以下为你介绍几种常见的处理文件重命名的方法:
pythonimport requests
from datetime import datetime
def download_file_with_timestamp(url, save_directory):
response = requests.get(url, stream=True)
file_name = url.split('/')[-1] # 获取原始文件名
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
new_file_name = f"{file_name}_{timestamp}"
save_path = f"{save_directory}/{new_file_name}"
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return save_path
# 使用示例
file_url = "https://example.com/file.txt"
save_dir = "C:/Downloads"
download_file_with_timestamp(file_url, save_dir)
pythonimport requests
counter = 1
def download_file_with_sequence(url, save_directory):
global counter
response = requests.get(url, stream=True)
file_name = url.split('/')[-1]
file_extension = file_name.split('.')[-1]
new_file_name = f"file_{counter}.{file_extension}"
counter += 1
save_path = f"{save_directory}/{new_file_name}"
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return save_path
# 多次调用该函数下载文件时,文件名将按顺序编号
pythonimport requests
from bs4 import BeautifulSoup
def download_webpage_with_title(url, save_directory):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
page_title = soup.title.string.strip().replace(' ', '_').replace('/', '_') # 清理标题中的非法字符
file_extension = 'html'
new_file_name = f"{page_title}.{file_extension}"
save_path = f"{save_directory}/{new_file_name}"
with open(save_path, 'w', encoding='utf-8') as f:
f.write(response.text)
return save_path
pythonimport json
def load_rename_config(config_file):
with open(config_file, 'r') as f:
config = json.load(f)
return config
def download_file_with_config(url, save_directory, config_file):
config = load_rename_config(config_file)
# 根据配置文件中的规则进行重命名和下载操作
# 假设配置文件中有一个 'prefix' 字段用于指定文件名前缀
prefix = config.get('prefix', '')
# 下载和重命名逻辑(结合前面提到的方法)
#...