So basically I’m trying to scrape a webpage with Python and I’m getting stucked at finding the number of childs of one element in a list using BeautifulSoup, the HTML of the list follows this:
JavaScript
x
18
18
1
<table id="MyTable">
2
<thead>
3
<tr></tr>
4
</thead>
5
<tfoot>
6
<tr></tr>
7
</tfoot>
8
<tbody>
9
<tr></tr>
10
<tr></tr>
11
<tr></tr>
12
<tr></tr>
13
<tr></tr>
14
<tr></tr>
15
<tr></tr>
16
</tbody>
17
</table>
18
In my case I want to get the number of tr
inside tag tbody
, but since it has no id, I found no way to get it with soup and then do a findAll("tr")
. Pretty sure this should be easy but can’t find a way, how should I do it?
Advertisement
Answer
You could check the len()
of your resultSet
from selecting all <tr>
in the <tbody>
:
JavaScript
1
2
1
len(soup.select('#MyTable > tbody tr'))
2
Example
JavaScript
1
26
26
1
from bs4 import BeautifulSoup
2
3
html='''
4
<table id="MyTable">
5
<thead>
6
<tr></tr>
7
</thead>
8
<tfoot>
9
<tr></tr>
10
</tfoot>
11
<tbody>
12
<tr></tr>
13
<tr></tr>
14
<tr></tr>
15
<tr></tr>
16
<tr></tr>
17
<tr></tr>
18
<tr></tr>
19
</tbody>
20
</table>
21
'''
22
23
soup = BeautifulSoup(html)
24
25
len(soup.select('#MyTable > tbody tr'))
26
Output
JavaScript
1
2
1
7
2