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
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)
通常修改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)
| 归档时间: |
|
| 查看次数: |
10940 次 |
| 最近记录: |