python http下载
时间: 2023-03-25 18:02:20 浏览: 53
可以使用Python的urllib库或requests库来进行http下载。例如,使用requests库可以这样实现:
```python
import requests
url = 'http://example.com/file.zip'
response = requests.get(url)
with open('file.zip', 'wb') as f:
f.write(response.content)
```
其中,url为要下载的文件的链接,response为请求的响应对象,response.content为响应内容的二进制形式,通过将其写入文件即可完成下载。
相关问题
python批量下载http
你可以使用Python的requests和os模块来批量下载HTTP文件。以下是示例代码:
```python
import requests
import os
# 从此URL列表下载文件
url_list = [
'http://example.com/file1.txt',
'http://example.com/file2.txt',
'http://example.com/file3.txt'
]
# 循环遍历URL列表并下载文件
for url in url_list:
# 获取文件名
file_name = os.path.basename(url)
# 发送GET请求并获取响应内容
response = requests.get(url)
# 将响应内容写入文件
with open(file_name, 'wb') as f:
f.write(response.content)
```
请注意,此代码将文件保存在当前工作目录中。如果要将文件保存到不同的目录中,请更改`open`函数中的文件路径。
python requests下载http压缩包
要使用Python中的requests库下载HTTP压缩包,您可以按照以下步骤进行操作:
1. 首先,导入requests库:`import requests`
2. 使用requests库发送GET请求并获取响应:`response = requests.get(url)`
3. 检查响应的状态码是否为200,表示请求成功:`if response.status_code == 200:`
4. 获取压缩包的内容:`content = response.content`
5. 将获取的内容保存到文件中,可以使用二进制模式打开文件,然后写入内容:`with open('filename.zip', 'wb') as file:` `file.write(content)`
下面是一个完整的示例代码:
```python
import requests
url = 'http://example.com/example.zip' # 替换为要下载的压缩包的URL
response = requests.get(url)
if response.status_code == 200:
content = response.content
with open('filename.zip', 'wb') as file:
file.write(content)
```
注意:在替换URL时,请确保提供的URL是指向一个有效的压缩包文件。