python response.text解析
时间: 2023-07-19 20:30:26 浏览: 277
在Python中,我们通常使用requests库来发送HTTP请求,获取网页的HTML内容。requests库提供了一个response对象,其中包含了从服务器返回的所有信息,包括状态码、响应头、响应正文等。
我们可以使用response.text属性来获取响应正文的字符串。接着,我们可以使用各种解析库(如BeautifulSoup、lxml等)来解析HTML文档,提取出我们需要的信息。
例如,以下代码使用requests库获取了CSDN首页的HTML内容,并使用BeautifulSoup解析出了页面中所有的h2标签:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.csdn.net/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
h2_tags = soup.find_all('h2')
print(h2_tags)
```
需要注意的是,response.text返回的是Unicode编码的字符串,如果页面使用了其他编码(如UTF-8、GB2312等),需要手动指定编码方式。可以使用response.encoding属性查看页面编码,或者使用response.apparent_encoding属性自动检测编码。例如:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.baidu.com/'
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title.string)
```
在这个例子中,我们手动指定了页面编码为UTF-8,然后使用BeautifulSoup解析出了页面的标题。
阅读全文
相关推荐


















