Beautifulsoup 4:删除评论标签及其内容

Fli*_*int 14 html python beautifulsoup html-parsing web-scraping

所以我正在报废的页面包含这些HTML代码.如何<!-- -->使用bs4删除评论标记及其内容?

<div class="foo">
cat dog sheep goat
<!-- 
<p>NewPP limit report
Preprocessor node count: 478/300000
Post?expand include size: 4852/2097152 bytes
Template argument size: 870/2097152 bytes
Expensive parser function count: 2/100
ExtLoops count: 6/100
</p>
-->

</div>
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 22

你可以使用extract()(解决方案基于这个答案):

PageElement.extract()从树中删除标记或字符串.它返回提取的标记或字符串.

from bs4 import BeautifulSoup, Comment

data = """<div class="foo">
cat dog sheep goat
<!--
<p>test</p>
-->
</div>"""

soup = BeautifulSoup(data)

div = soup.find('div', class_='foo')
for element in div(text=lambda text: isinstance(text, Comment)):
    element.extract()

print soup.prettify()
Run Code Online (Sandbox Code Playgroud)

结果你得到了你div没有评论:

<div class="foo">
    cat dog sheep goat
</div>
Run Code Online (Sandbox Code Playgroud)

  • 太棒了..这就像魅力一样,但我在尝试理解 `div(text=lambda text: isinstance(text, Comment)):` 时遇到了问题。你能详细说明一下它是如何工作的吗? (2认同)

roi*_*ppi 6

通常修改bs4解析树是不必要的.你可以得到div的文本,如果这是你想要的:

soup.body.div.text
Out[18]: '\ncat dog sheep goat\n\n'
Run Code Online (Sandbox Code Playgroud)

bs4将评论分开.但是,如果您确实需要修改解析树:

from bs4 import Comment

for child in soup.body.div.children:
    if isinstance(child,Comment):
        child.extract()
Run Code Online (Sandbox Code Playgroud)