BeautifulSoup test object type

Viewed 685

Good afternoon,

I'm using BeautifulSoup to load and parse the content of an html file.

My input looks like this

<tbody id="data">

    <tr>
<td>
  some text </td>
</tr>

My code snippet looks like this

from bs4 import BeautifulSoup
with open('table.htm') as f:
    src_html=BeautifulSoup(f,"html.parser")
table=src_html.find(id="data")
type(table.contents[0])  # bs4.element.NavigableString
type(table.contents[1])  # bs4.element.Tag

Because my table has several cells I want to get the cells whose type is bs4.element.Tag, how can I do something like

for c in table.children:
    if type(c) is bs4.element.Tag then do something

Thanks for your help

Simon

1 Answers

I found a way to to answer to my question

from bs4.element import NavigableString, Tag
cells = [ t for t in table.contents if isinstance(t, Tag) ]
Related