Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup test object type

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

like image 258
Simon Avatar asked Nov 18 '25 16:11

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) ]
like image 168
Simon Avatar answered Nov 20 '25 07:11

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!