#输出页面信息 print(res) 输出网页代码乱码如何解决
时间: 2024-03-31 10:32:00 浏览: 14
如果输出的网页代码是乱码,可能是因为编码格式不正确导致的。你可以尝试使用网页的正确编码格式进行解码,例如:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求
url = 'http://www.example.com'
res = requests.get(url)
# 指定编码格式
res.encoding = 'utf-8'
# 解析网页
soup = BeautifulSoup(res.text, 'html.parser')
# 输出页面信息
print(soup.prettify())
```
在这个例子中,我们指定了编码格式为`utf-8`,如果你的网页编码格式不同,需要根据实际情况进行调整。使用正确的编码格式解码后,输出的网页代码应该就不会是乱码了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)