使用Python从网站下载所有Zip文件可以通过以下步骤实现:
import requests
import os
def download_zip(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as file:
file.write(response.content)
website_url = 'https://example.com' # 替换为目标网站的URL
response = requests.get(website_url)
zip_links = []
if response.status_code == 200:
zip_links = re.findall(r'<a href="(.*\.zip)">', response.text)
save_directory = 'zip_files' # 替换为保存Zip文件的目录路径
if not os.path.exists(save_directory):
os.makedirs(save_directory)
for link in zip_links:
zip_url = website_url + link
file_name = link.split('/')[-1]
save_path = os.path.join(save_directory, file_name)
download_zip(zip_url, save_path)
完整的代码如下:
import requests
import os
import re
def download_zip(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as file:
file.write(response.content)
website_url = 'https://example.com' # 替换为目标网站的URL
response = requests.get(website_url)
zip_links = []
if response.status_code == 200:
zip_links = re.findall(r'<a href="(.*\.zip)">', response.text)
save_directory = 'zip_files' # 替换为保存Zip文件的目录路径
if not os.path.exists(save_directory):
os.makedirs(save_directory)
for link in zip_links:
zip_url = website_url + link
file_name = link.split('/')[-1]
save_path = os.path.join(save_directory, file_name)
download_zip(zip_url, save_path)
这段代码通过使用requests库来发送HTTP请求并获取网页内容,使用正则表达式来提取所有Zip文件的链接。然后,通过循环遍历每个链接,使用定义的download_zip函数来下载并保存Zip文件到指定的目录中。
注意:在实际使用中,需要根据目标网站的具体情况进行适当的修改,例如修改网站URL、保存目录路径等。另外,还需要处理可能出现的异常情况,例如网络连接错误、文件保存失败等。
领取专属 10元无门槛券
手把手带您无忧上云