利用url获取超文本文件并保存至本地代码
时间: 2023-12-20 12:04:03 浏览: 75
以下是利用Python的requests库获取超文本文件并保存至本地的代码示例:
```python
import requests
url = "http://www.example.com" # 替换成你要获取的URL
filename = "example.html" # 替换成你想要保存的文件名
response = requests.get(url)
with open(filename, "wb") as f:
f.write(response.content)
```
这段代码中,我们首先定义了要获取的URL和保存的文件名。然后,使用`requests.get()`方法获取该URL对应的响应对象。最后,将响应对象的内容写入本地文件中。注意,这里使用了`wb`模式,即二进制写入模式,适用于保存非文本文件。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)