python根据链接下载文件到本地

python根据链接下载文件到本地

import requests

def download_file(url, local_filename):
    # 发送一个HTTP请求到URL
    response = requests.get(url)
    
    # 确保请求成功
    if response.status_code == 200:
        # 打开本地文件,准备写入数据
        with open(local_filename, 'wb') as f:
            # 写入响应的内容
            f.write(response.content)
    else:
        print(f"Failed to download file. HTTP Status Code: {response.status_code}")

使用函数下载文件

download_file('http://example.com/somefile.zip', 'localfile.zip')

你可能感兴趣的:(python,开发语言)