python requests 下载
时间: 2023-07-25 09:04:09 浏览: 125
Python的requests库可以用来下载文件。具体步骤如下:
1. 导入requests库
```python
import requests
```
2. 发送请求并获取响应
```python
url = 'http://example.com/file.zip'
response = requests.get(url)
```
3. 将响应内容写入文件
```python
with open('file.zip', 'wb') as f:
f.write(response.content)
```
其中,'wb'表示以二进制写入模式打开文件,可以保证下载的文件不会出现乱码。
完整代码如下:
```python
import requests
url = 'http://example.com/file.zip'
response = requests.get(url)
with open('file.zip', 'wb') as f:
f.write(response.content)
```
注意:在实际使用中,需要根据具体情况设置请求头、代理等参数,以确保下载的文件正确无误。
阅读全文
相关推荐















