Question

I have a html code:

<table>
<tr>
   <td><table><tr><td>1</td></tr><tr><td>2</td></tr></table></td>
</tr>
<tr>
   <td><table><tr><td>3</td></tr><tr><td>4</td></tr></table></td>
</tr>
</table>

I want to find all tr in first table. I usually using

for tr in soup.findAll('tr'):

But i will get all tr (tr in main table and in sub table). How to get tr in main table only?

Was it helpful?

Solution

How about this?

from bs4 import BeautifulSoup

soup = BeautifulSoup("""
<table>
<tr>
   <td><table><tr><td>1</td></tr><tr><td>2</td></tr></table></td>
</tr>
<tr>
   <td><table><tr><td>3</td></tr><tr><td>4</td></tr></table></td>
</tr>
</table>
""")

for tr in soup.find('table').find_all('tr', recursive=False):
    print tr

recursive=False helps to find only top-level tags (see docs).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top