如何用靓汤找到所有评论

2024-04-24 11:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

This question四年前就有人问过,但现在BS4的答案已经过时了。

我想删除所有的评论在我的html文件使用美丽的汤。由于BS4生成每个comment as a special type of navigable string,我认为这段代码可以工作:

for comments in soup.find_all('comment'):
     comments.decompose()

所以那没用。。。。如何使用BS4查找所有注释?


Tags: 文件of答案stringhtmlastypecomment
2条回答

可以传递函数find_all()来帮助它检查字符串是否为注释。

例如,我有以下html:

<body>
   <!-- Branding and main navigation -->
   <div class="Branding">The Science &amp; Safety Behind Your Favorite Products</div>
   <div class="l-branding">
      <p>Just a brand</p>
   </div>
   <!-- test comment here -->
   <div class="block_content">
      <a href="https://www.google.com">Google</a>
   </div>
</body>

代码:

from bs4 import BeautifulSoup as BS
from bs4 import Comment
....
soup = BS(html, 'html.parser')
comments = soup.find_all(string=lambda text: isinstance(text, Comment))
for c in comments:
    print(c)
    print("===========")
    c.extract()

结果将是:

Branding and main navigation 
============
test comment here
============

顺便说一句,我认为find_all('Comment')不起作用的原因是(来自BeautifulSoup文档):

Pass in a value for name and you’ll tell Beautiful Soup to only consider tags with certain names. Text strings will be ignored, as will tags whose names that don’t match.

我需要做两件事:

第一,进口靓汤时

from bs4 import BeautifulSoup, Comment

其次,下面是提取注释的代码

for comments in soup.findAll(text=lambda text:isinstance(text, Comment)):
    comments.extract()

相关问题 更多 >