问题描述: 在使用BeautifulSoup库的Python的.text方法时,发现它不返回正确的文本。
解决方法:
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='编码方式')
from bs4 import BeautifulSoup
# 创建BeautifulSoup对象
soup = BeautifulSoup(html_doc, 'html.parser')
# 使用.get_text()方法获取文本内容
text = soup.get_text()
print(text)
from bs4 import BeautifulSoup
# 创建BeautifulSoup对象
soup = BeautifulSoup(html_doc, 'html.parser')
# 使用.strip()方法去除空白字符
text = soup.text.strip()
print(text)
使用上述方法,可以解决BeautifulSoup库的.text方法不返回正确的文本的问题。