Home > Software design >  filter <tr> elements in HTML without class attribute
filter <tr> elements in HTML without class attribute

Time:10-08

I want to get all tr elements that has class = "even" only and not "even detailRow detailRow-1" ?

I usually filter any div/tr elements using class attrb like this (just an exmaple)

rows = soup.find_all('div', class_='ca-fortnight234')

but don't want to use class attrb here, as it is so common text (even).

Please suggest

enter image description here

Adding sample html

 <tbody style="">
            <tr __gwt_row="0" __gwt_subrow="0" class=" even">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa fa-plus-square-o fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153">
               07/15/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154">
               07/15/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155">
               Payment Applied
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156">
               $386.36
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157">
               $620.19
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158">
               $416.82
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159">
               $1,423.37
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160">
               $2,501.51
              </div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161">
               $47
              </div></td>
            </tr>
            <tr __gwt_row="1" __gwt_subrow="0" class=" even detailRow detailRow-1">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa  fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160"></div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161"></div></td>
            </tr>
            <tr __gwt_row="2" __gwt_subrow="0" class=" odd">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa fa-plus-square-o fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153">
               07/02/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154">
               07/02/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155">
               Mortgage Insurance Disbursement
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156">
               $0.00
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157">
               $0.00
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158">
               ($133.02)
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159">
               ($133.02)
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160">
               $2,084.69
              </div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161">
               $192,058.49
              </div></td>
            </tr>
            <tr __gwt_row="3" __gwt_subrow="0" class=" odd detailRow detailRow-3">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa  fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160"></div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161"></div></td>
            </tr>
            <tr __gwt_row="4" __gwt_subrow="0" class=" even">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa fa-plus-square-o fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153">
               06/15/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154">
               06/15/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155">
               Payment Applied
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156">
               $385.12
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157">
               $621.43
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158">
               $416.82
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159">
               $1,423.37
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160">
               $2,217.71
              </div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161">
               $192,058.49
              </div></td>
            </tr>
            <tr __gwt_row="5" __gwt_subrow="0" class=" even detailRow detailRow-5">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa  fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160"></div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161"></div></td>
            </tr>
            <tr __gwt_row="6" __gwt_subrow="0" class=" odd">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa fa-plus-square-o fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153">
               06/02/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154">
               06/02/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155">
               Mortgage Insurance Disbursement
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156">
               $0.00
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157">
               $0.00
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158">
               ($133.02)
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159">
               ($133.02)
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160">
               $1,800.89
              </div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161">
               $192,443.61
              </div></td>
            </tr>
            <tr __gwt_row="7" __gwt_subrow="0" class=" odd detailRow detailRow-7">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa  fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160"></div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161"></div></td>
            </tr>
            <tr __gwt_row="8" __gwt_subrow="0" class=" even">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa fa-plus-square-o fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153">
               05/14/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154">
               05/14/2021
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155">
               Payment Applied
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156">
               $383.88
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157">
               $622.67
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158">
               $416.82
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159">
               $1,423.37
              </div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160">
               $1,933.91
              </div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161">
               $192,443.61
              </div></td>
            </tr>
            <tr __gwt_row="9" __gwt_subrow="0" class=" even detailRow detailRow-9">
             <td class="gwtb3-cell gwtb3-d gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-152">
               <button class="db-link-btn link-btn-full" aria-label="Expand additional information"><span class="fa  fa-lg ccn-fa-expander"></span></button>
              </div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-153"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-154"></div></td>
             <td class="gwtb3-cell gwtb3-d">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-155"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-156"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-157"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-158"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-159"></div></td>
             <td class="gwtb3-cell gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-160"></div></td>
             <td class="gwtb3-cell gwtb3-d gwtb3-d" align="right">
              <div style="outline-style:none;" __gwt_cell="cell-gwt-uid-161"></div></td>
            </tr>

CodePudding user response:

I still think searching for tr tag with class==even is the best approach.

As an alternative and given that for even rows value for __gwt_row % 4 is 0, you can use something like

even_rows = soup.find_all(lambda x: x.name == 'tr' and not int(x.get('__gwt_row', 1)) % 4)

CodePudding user response:

You can use this:

mytr = soup.find_all("tr", {"class": "even"})

This will pull all Table Rows which have the class even. It will not pull the Table Rows which also have other classes

CodePudding user response:

Using ElementTree (just specify the attribute value)

import xml.etree.ElementTree as ET


html = '''<table><tbody>
         <tr class="even plus"></tr>
         <tr class="even"></tr>
         <tr class="even plus"></tr>
         <tr class="even"></tr>
         <tr class="even plus"></tr>
         </tbody></table>'''
root = ET.fromstring(html)
rows = root.findall('.//tr[@class="even"]')
print(rows)

output

[<Element 'tr' at 0x7f9705883c70>, <Element 'tr' at 0x7f9705883d10>]

CodePudding user response:

If you want to filter via the attribute of the tr, you need this:

mytr = soup.find_all("tr", {"__gwt_row": "9"})

or if you want to filter via the subrow attribute it would be

mytr = soup.find_all("tr" ,{"__gwt_subrow": "0"})
  • Related