Reputation: 19648
Say I have a html table like this:
<table>
<tr>
<tr>...
</tr>
<tr>...
</tr>
</tr>
<tr>
<tr>...
</tr>
<tr>...
</tr>
</tr>
...
</table>
I could locate the table tag. How could I find the first layer of table rows which are the son .. not the grandson of the table.
print table.findAll('tr') # would return All the trs under table which is not what I want.
Upvotes: 2
Views: 779
Reputation: 369424
Try following:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''
<body>
<table>
<tr id="tr_1">
<tr id="tr_1_1">..</tr>
<tr id="tr_1_2">...</tr>
</tr>
<tr id="tr_2">
<tr id="tr_2_1">...</tr>
<tr id="tr_2_2">...</tr>
</tr>
</table>
</body>''', ['lxml','xml'])
for tr in soup.select('table > tr'):
print(tr)
print('---')
prints
<tr id="tr_1">
<tr id="tr_1_1">..</tr>
<tr id="tr_1_2">...</tr>
</tr>
---
<tr id="tr_2">
<tr id="tr_2_1">...</tr>
<tr id="tr_2_2">...</tr>
</tr>
---
NOTE: need lxml
.
Upvotes: 2