要获取真实的文本内容,可以使用BeautifulSoup库的get_text()方法。下面是一个示例代码:
import requests
from bs4 import BeautifulSoup
# 发起HTTP请求
response = requests.get('http://example.com')
# 创建BeautifulSoup对象
soup = BeautifulSoup(response.text, 'html.parser')
# 提取真实的文本内容
text = soup.get_text()
# 打印文本内容
print(text)
在上述示例中,首先使用requests库发起HTTP请求来获取网页源代码。然后使用BeautifulSoup库创建一个BeautifulSoup对象,将网页源代码作为参数传入。最后使用get_text()方法提取真实的文本内容,并将其打印出来。