Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>thanks to <a href="https://stackoverflow.com/users/4279/j-f-sebastian">J.F.Sebastian</a> , the following will work: </p> <pre><code>rows=table.tbody.find_all(True, recursive=False) </code></pre> <p>Documentation here: <a href="http://www.crummy.com/software/BeautifulSoup/bs4/doc/#true" rel="nofollow noreferrer">http://www.crummy.com/software/BeautifulSoup/bs4/doc/#true</a></p> <p>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: </p> <pre><code>rows=table.tbody.find_all('tr') </code></pre> <p>Again, docs: <a href="http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-using-tag-names" rel="nofollow noreferrer">http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-using-tag-names</a></p> <p>I believe this is a better way than iterating through all the children of a Tag. </p> <p>Worked with the following input: </p> <pre><code>&lt;table cellspacing="0" cellpadding="0"&gt; &lt;thead&gt; &lt;tr class="title-row"&gt; &lt;th class="title" colspan="100"&gt; &lt;div style="position:relative;"&gt; President &lt;span class="pct-rpt"&gt; 99% reporting &lt;/span&gt; &lt;/div&gt; &lt;/th&gt; &lt;/tr&gt; &lt;tr class="header-row"&gt; &lt;th class="photo first"&gt; &lt;/th&gt; &lt;th class="candidate "&gt; Candidate &lt;/th&gt; &lt;th class="party "&gt; Party &lt;/th&gt; &lt;th class="votes "&gt; Votes &lt;/th&gt; &lt;th class="pct "&gt; Pct. &lt;/th&gt; &lt;th class="change "&gt; Change from &amp;lsquo;08 &lt;/th&gt; &lt;th class="evotes last"&gt; Electoral Votes &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class=""&gt; &lt;td class="photo first"&gt; &lt;div class="photo_wrap"&gt;&lt;img alt="P-barack-obama" height="48" src="http://i1.nyt.com/projects/assets/election_2012/images/candidate_photos/election_night/p-barack-obama.jpg?1352320690" width="68" /&gt;&lt;/div&gt; &lt;/td&gt; &lt;td class="candidate "&gt; &lt;div class="winner dem"&gt;&lt;img alt="Hp-checkmark@2x" height="9" src="http://i1.nyt.com/projects/assets/election_2012/images/swatches/hp-checkmark@2x.png?1352320690" width="10" /&gt;Barack Obama&lt;/div&gt; &lt;/td&gt; &lt;td class="party "&gt; Dem. &lt;/td&gt; &lt;td class="votes "&gt; 2,916,811 &lt;/td&gt; &lt;td class="pct "&gt; 57.3% &lt;/td&gt; &lt;td class="change "&gt; -4.6% &lt;/td&gt; &lt;td class="evotes last"&gt; 20 &lt;/td&gt; &lt;/tr&gt; &lt;tr class=""&gt; &lt;td class="photo first"&gt; &lt;/td&gt; &lt;td class="candidate "&gt; &lt;div class="not-winner"&gt;Mitt Romney&lt;/div&gt; &lt;/td&gt; &lt;td class="party "&gt; Rep. &lt;/td&gt; &lt;td class="votes "&gt; 2,090,116 &lt;/td&gt; &lt;td class="pct "&gt; 41.1% &lt;/td&gt; &lt;td class="change "&gt; +4.3% &lt;/td&gt; &lt;td class="evotes last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;tr class=""&gt; &lt;td class="photo first"&gt; &lt;/td&gt; &lt;td class="candidate "&gt; &lt;div class="not-winner"&gt;Gary Johnson&lt;/div&gt; &lt;/td&gt; &lt;td class="party "&gt; Lib. &lt;/td&gt; &lt;td class="votes "&gt; 54,798 &lt;/td&gt; &lt;td class="pct "&gt; 1.1% &lt;/td&gt; &lt;td class="change "&gt; &amp;ndash; &lt;/td&gt; &lt;td class="evotes last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;tr class="last-row"&gt; &lt;td class="photo first"&gt; &lt;/td&gt; &lt;td class="candidate "&gt; div class="not-winner"&gt;Jill Stein&lt;/div&gt; &lt;/td&gt; &lt;td class="party "&gt; Green &lt;/td&gt; &lt;td class="votes "&gt; 29,336 &lt;/td&gt; &lt;td class="pct "&gt; 0.6% &lt;/td&gt; &lt;td class="change "&gt; &amp;ndash; &lt;/td&gt; &lt;td class="evotes last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="footer" colspan="100"&gt; &lt;a href="/2012/results/president"&gt;President Map&lt;/a&gt; &amp;nbsp;|&amp;nbsp; &lt;a href="/2012/results/president/big-board"&gt;President Big Board&lt;/a&gt;&amp;nbsp;|&amp;nbsp; &lt;a href="/2012/results/president/exit-polls?state=il"&gt;Exit Polls&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload