请给出完整的fetch_html内容
时间: 2024-09-24 09:15:14 浏览: 32
基于你的描述,我们可以对`fetch_html`函数进行一些修改,使其能够从远程目录下载以"kylin-update-desktop-config_"开头,同时结束于".dsc"或".tar.xz"的文件。这里是修改后的完整版本:
```python
import os
import requests
from urllib.parse import urljoin
def fetch_html(config_address, local_dir='local_page', local_file_prefix='kylin-update-desktop-config_', extensions=['.dsc', '.tar.xz']):
# 构建URL
url = f"{config_address}/files/"
# 发起GET请求以获取目录内容
response = requests.get(url, headers={'Access-Email': EMAIL, 'Access-Token': ACCESS_TOKEN}, verify=False)
if response.status_code == 200:
try:
# 解析JSON数据获取文件名列表
json_data = response.json()
available_files = [file for file in json_data.keys() if file.startswith(local_file_prefix) and any(file.endswith(ext) for ext in extensions)]
# 创建目标文件夹(如果有需要)
os.makedirs(local_dir, exist_ok=True)
# 对每个找到的文件进行下载
for file_name in available_files:
file_url = urljoin(url, file_name)
full_path = os.path.join(local_dir, file_name)
# 下载单个文件
response_single = requests.get(file_url, headers=headers, verify=False)
if response_single.status_code == 200:
with open(full_path, 'wb') as file:
file.write(response_single.content)
print(f"Downloaded {file_url} to {full_path}")
else:
print(f"Failed to download {file_url}: {response_single.status_code}")
except Exception as e:
print(f"Error parsing response: {e}")
else:
print(f"Failed to retrieve directory content: {response.status_code}")
# 使用示例
fetch_html(default_config_address)
```
在这个版本里,我们添加了`extensions`参数来存储允许的文件扩展名,以及通过`response.json()`获取远程目录的内容。然后筛选出符合条件的文件进行下载。
阅读全文