要使用BeautifulSoup按id取消注释,可以按照以下步骤操作:
from bs4 import BeautifulSoup
html = """
Example
This is a paragraph.
"""
soup = BeautifulSoup(html, 'html.parser')
paragraph_tag = soup.find(id='paragraph')
if paragraph_tag:
comments = paragraph_tag.find_all(text=lambda text: isinstance(text, Comment))
for comment in comments:
comment.extract()
完整的代码示例如下:
from bs4 import BeautifulSoup
from bs4.element import Comment
html = """
Example
This is a paragraph.
"""
soup = BeautifulSoup(html, 'html.parser')
paragraph_tag = soup.find(id='paragraph')
if paragraph_tag:
comments = paragraph_tag.find_all(text=lambda text: isinstance(text, Comment))
for comment in comments:
comment.extract()
print(soup.prettify())
运行以上代码,将会打印出取消注释后的HTML内容:
Example
This is a paragraph.