Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the HTML-pattern like

<p class='cls1'> Hello </p>

So I want to find the tag <p> (and I don't know what tag is around the text for now) by searching "Hello" using BS4.

It should be something like this:

full_string = soup.find(text=re.compile('Hello'))

full_string.get_parent_tag() # <p>

full_string.get_parent_class() # cls1

1 Answer

0 votes
by (36.8k points)

It's possible.

import re

from bs4 import BeautifulSoup

your_html = """<p class='cls1'> Hello </p>"""

print(BeautifulSoup(your_html, "html.parser").find_all(lambda t: t.name == "p" and re.compile("Hello")))

Output:

[<p class="cls1"> Hello </p>]

If you don't know the tag you're after, you could try this:

from lxml import html

your_html = """<p class='cls1'> Hello </p>"""

print(html.fromstring(your_html).xpath("//*[contains(text(), 'Hello')]"))

Output:

[<Element p at 0x7f2b172ae5e0>]

 Want to be a master in Data Science? Enroll in this Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...