Adnos
Adnos

Reputation: 9

Navigating through html with BeautifulSoup from a specific point

I'm using the following piece of code to find an attribute in a piece of HTML code:

results = soup.findAll("svg", {"data-icon" : "times"})

This works, and it returns me a list with the tag and attributes. However, I would also like to move from that part of the HTML code, to the sibling (if that's the right term) below it, and retrieve the contents of that paragraph. See the example below.

<div class="382"><svg aria-hidden="true" data-icon="times".......</svg></div>
<div class="405"><p>Example</p></div>

I can't seem to figure out how to do this properly. Searching for the div class names does not work, because the class name is randomised.

Upvotes: 1

Views: 618

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195553

You can use CSS selector with +:

from bs4 import BeautifulSoup

html_doc = """
<div class="382"><svg aria-hidden="true" data-icon="times"> ... </svg></div>
<div class="405"><p>Example</p></div>
"""

soup = BeautifulSoup(html_doc, "html.parser")

div = soup.select_one('div:has(svg[data-icon="times"]) + div')
print(div.text)

Prints:

Example

Or without CSS selector:

div = soup.find("svg", attrs={"data-icon": "times"}).find_next("div")
print(div.text)

Prints:

Example

Upvotes: 2

Related Questions