要修复BeautifulSoup返回的结果中不包含空评论的问题,可以使用以下解决方法:
from bs4 import BeautifulSoup
# 假设html是包含评论的HTML代码
html = '''
评论2
'''
soup = BeautifulSoup(html, 'html.parser')
comments = [comment.get_text() for comment in soup.find_all('p') if comment.get_text().strip()]
print(comments)
输出结果:
['评论1', '评论2']
在上面的例子中,我们使用find_all()方法找到所有的标签,然后使用列表推导式过滤掉空评论。最后打印输出了过滤后的评论列表。
from bs4 import BeautifulSoup
# 假设html是包含评论的HTML代码
html = '''
评论1
评论2
'''
soup = BeautifulSoup(html, 'html.parser')
empty_comments = soup.find_all('p', text=lambda text: text and not text.strip())
for comment in empty_comments:
comment.extract()
comments = [comment.get_text() for comment in soup.find_all('p')]
print(comments)
输出结果:
['评论1', '评论2']
在上面的例子中,我们使用find_all()方法找到所有的标签,并通过设置text参数为一个lambda函数,将空评论找到。然后使用extract()方法将这些空评论从解析树中删除。最后,我们再次使用find_all()方法找到所有的评论,并打印输出过滤后的评论列表。
评论1