python爬虫下载文件
时间: 2023-12-20 12:05:23 浏览: 160
以下是使用Python爬虫下载文件的示例代码:
```python
import requests
url = 'http://example.com/file.pdf' # 文件的URL地址
filename = 'file.pdf' # 下载后保存的文件名
response = requests.get(url)
with open(filename, 'wb') as file:
file.write(response.content)
print('文件下载完成!')
```
这段代码使用了`requests`库来发送HTTP请求并获取文件内容,然后将文件内容写入到本地文件中。你只需要将`url`替换为你要下载的文件的URL地址,将`filename`替换为你想要保存的文件名,运行代码即可完成文件的下载。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)