Skip to content
Advertisement

Getting number of childs of an element with no ID with beatifulsoup

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:

<table id="MyTable">
   <thead>
      <tr>...</tr>
   </thead>
   <tfoot>
      <tr>...</tr>
   </tfoot>
   <tbody>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
   </tbody>
</table>

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>:

len(soup.select('#MyTable > tbody tr'))

Example

from bs4 import BeautifulSoup

html='''
<table id="MyTable">
   <thead>
      <tr>...</tr>
   </thead>
   <tfoot>
      <tr>...</tr>
   </tfoot>
   <tbody>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
      <tr>...</tr>
   </tbody>
</table>
'''

soup = BeautifulSoup(html)

len(soup.select('#MyTable > tbody tr'))

Output

7
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement