Linux 拨号vps windows公众号手机端

python怎么通过url下载文件

lewis 9年前 (2016-11-14) 阅读数 12 #程序编程
文章标签 python

你可以使用urllibrequests模块来下载文件。以下是使用urllib模块的示例代码:

import urllib.request

url = "https://example.com/sample.jpg"
filename = "sample.jpg"

urllib.request.urlretrieve(url, filename)
print("文件已下载")

如果你希望使用requests模块来下载文件,可以参考以下示例代码:

import requests

url = "https://example.com/sample.jpg"
filename = "sample.jpg"

response = requests.get(url)
with open(filename, "wb") as file:
    file.write(response.content)

print("文件已下载")

这两种方法都可以用来从URL下载文件,你可以根据自己的偏好选择其中一种。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门