Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using BeautifulSoup, Can I quickly traverse to a specific parent element?

Say I reference an element inside of a table in a HTML page like this:

someEl = soup.findAll(text = "some text")

I know for sure this element is embedded inside a table, is there a way to find the parent table without having to call .parent so many times?

<table...>

..
..
<tr>....<td><center><font..><b>some text</b></font></center></td>....<tr>

<table>
like image 637
Blankman Avatar asked Dec 12 '25 07:12

Blankman


2 Answers

Check out findParents, it has a similar form to findAll:

soup = BeautifulSoup("<table>...</table>")

for text in soup.findAll(text='some text')
  table = text.findParents('table')[0]
  # table is your now your most recent `<table>` parent

Here are the docs for findAllPrevious and also findParents.

like image 183
Jesse Dhillon Avatar answered Dec 14 '25 21:12

Jesse Dhillon


while someEl.name != "table":
    someEl = someEl.parent
# someEl is now the table
like image 31
icktoofay Avatar answered Dec 14 '25 20:12

icktoofay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!