问题描述:在使用BeautifulSoup解析HTML文档时,尝试从包装的div中获取文本,但结果要么为空,要么为"None"。
解决方法:
pip install beautifulsoup4
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
div = soup.find('div', {'class': 'wrapper'}) # 使用CSS选择器查找
div = soup.find_all('div')[0] # 使用标签名称查找
if div is not None and div.text.strip() != "":
# 执行相应的操作
text = div.text.strip()
print(text)
else:
print("无法获取文本内容")
请根据实际情况修改代码中的div查找方式和操作。