Alex Rivera | Logout

beautiful soup get children that are Tags (not Navigable Strings) from a Tag

Asked 2012-11-06T16:29:24.480
15

Beautiful soup documentation provides attributes .contents and .children to access the children of a given tag (a list and an iterable respectively), and includes both Navigable Strings and Tags. I want only the children of type Tag.

I'm currently accomplishing this using list comprehension:

rows=[x for x in table.tbody.children if type(x)==bs4.element.Tag]

but I'm wondering if there is a better/more pythonic/built-in way to get just Tag children.

Edit
Report

1 Answer

17

thanks to J.F.Sebastian , the following will work:

rows=table.tbody.find_all(True, recursive=False)

Documentation here: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#true

In my case, I needed actual rows in the table, so I ended up using the following, which is more precise and I think more readable:

rows=table.tbody.find_all('tr')

Again, docs: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-using-tag-names

I believe this is a better way than iterating through all the children of a Tag.

Worked with the following input:

<table cellspacing="0" cellpadding="0">
  <thead>
    <tr class="title-row">
      <th class="title" colspan="100">
        <div style="position:relative;">
          President
            <span class="pct-rpt">
                99% reporting
            </span>
        </div>
      </th>
    </tr>
    <tr class="header-row">
        <th class="photo first">

        </th>
        <th class="candidate ">
          Candidate
        </th>
        <th class="party ">
          Party
        </th>
        <th class="votes ">
          Votes
        </th>
        <th class="pct ">
          Pct.
        </th>
        <th class="change ">
          Change from &lsquo;08
        </th>
        <th class="evotes last">
          Electoral Votes
        </th>
    </tr>
  </thead>
  <tbody>
      <tr class="">
          <td class="photo first">
            <di
answered 2012-11-08T15:08:30.877

Your Answer