You just need to iterate through all the available comments to see if this is one of your required entries, and then display the text for the next element as follows:
from bs4 import BeautifulSoup, Comment
html = """
<html>
<body>
<p>p tag text</p>
I would like to get this text
I would also like to find this text
</body>
</html>
"""
soup = BeautifulSoup(html, 'lxml')
for comment in soup.findAll(text=lambda text:isinstance(text, Comment)):
if comment in ['UNIQUE COMMENT', 'SECOND UNIQUE COMMENT']:
print comment.next_element.strip()
The following will appear:
I would like to get this text
I would also like to find this text
source
share