Home > Enterprise >  Navigating through html with BeautifulSoup from a specific point
Navigating through html with BeautifulSoup from a specific point

Time:03-29

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 ><svg aria-hidden="true" data-icon="times".......</svg></div>
<div ><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.

CodePudding user response:

You can use CSS selector with :

from bs4 import BeautifulSoup

html_doc = """
<div ><svg aria-hidden="true" data-icon="times"> ... </svg></div>
<div ><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
  • Related