Ekanshu
Ekanshu

Reputation: 69

Get text after specific text with beautiful soup

I have to get the full address from the below HTML code.

html_text = """
<div><h2 class="rounded">Address</h2><div class="textwidget"><p>30 Dov Hoz<br />
Kiryat Ono<br />
Israel 5555626</p>
</div></div>
"""

First I just want to search "Address" keyword in the HTML code and if it is there then I want to get all the text after Address keyword. I cannot use the class tag or any other tag because there are many classes with the same class name.

My code:

html_text = """
<div><h2 class="rounded">Address</h2><div class="textwidget"><p>30 Dov Hoz<br />
Kiryat Ono<br />
Israel 5555626</p>
</div></div>
"""

soup = BeautifulSoup(html_text, 'html.parser')
label = soup.find(text='Address')
print(lable)
add= label.next_sibling
print(add)

But I am getting None from the above code.

My desired output is like this:

Address
30 Dov Hoz
Kiryat Ono
Israel 5555626

Upvotes: 1

Views: 1015

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195438

Another solution, using CSS selector:

soup = BeautifulSoup(html_text, 'html.parser')
print(soup.select_one('h2:contains("Address") + div').text)

Prints:

30 Dov Hoz
Kiryat Ono
Israel 5555626

Upvotes: 0

Rakesh
Rakesh

Reputation: 82765

Use .findNext("div")

Ex:

soup = BeautifulSoup(html_text, 'html.parser')
label = soup.find(text='Address').findNext("div").text
print(label)

Or next_element

Ex:

label = soup.find(text='Address').next_element.text
print(label)

Output:

30 Dov Hoz
Kiryat Ono
Israel 5555626

Upvotes: 1

Related Questions