2012-11-06 11 views
10

La bella zuppa di documentazione fornisce attributi .contents e .children per accedere ai figli di un determinato tag (un elenco e un iterable rispettivamente) e include entrambe le stringhe navigabili e tag. Voglio solo i bambini di tipo Tag.bella zuppa ottenere i bambini che sono tag (non stringhe navigabili) da un tag

Attualmente sto compiendo questa lista di comprensione utilizzando:

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

ma mi chiedo se c'è un// modo migliore più divinatorio built-in per ottenere solo Tag bambini.

+5

'table.tbody.find_all (True, recursive = False)' (non ho provato) – jfs

+0

[si potrebbe pubblicare il tuo proprio answer] (http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/): include il link ai documenti, esempio di codice eseguibile con input/output – jfs

risposta

14

grazie alla J.F.Sebastian, il seguente funziona:

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

documentazione qui: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#true

Nel mio caso, ho bisogno di file effettivi della tabella, quindi ho finito per usare il seguente, che è più precisa e penso che più leggibile:

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

Anche in questo caso, docs: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-using-tag-names

Credo che questo sia un modo migliore di scorrere tutti i figli di un Tag.

lavorato con la seguente digitazione:

<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"> 
      <div class="photo_wrap"><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" /></div> 
      </td> 
      <td class="candidate "> 
      <div class="winner dem"><img alt="[email protected]" height="9" src="http://i1.nyt.com/projects/assets/election_2012/images/swatches/[email protected]?1352320690" width="10" />Barack Obama</div> 
      </td> 
      <td class="party "> 
      Dem. 
      </td> 
      <td class="votes "> 
      2,916,811 
      </td> 
      <td class="pct "> 
      57.3% 
      </td> 
      <td class="change "> 
      -4.6% 
      </td> 
      <td class="evotes last"> 
      20 
      </td> 
     </tr> 
     <tr class=""> 
      <td class="photo first"> 

      </td> 
      <td class="candidate "> 
      <div class="not-winner">Mitt Romney</div> 
      </td> 
      <td class="party "> 
      Rep. 
      </td> 
      <td class="votes "> 
      2,090,116 
      </td> 
      <td class="pct "> 
      41.1% 
      </td> 
      <td class="change "> 
      +4.3% 
      </td> 
      <td class="evotes last"> 
      0 
      </td> 
     </tr> 
     <tr class=""> 
      <td class="photo first"> 

      </td> 
      <td class="candidate "> 
      <div class="not-winner">Gary Johnson</div> 
      </td> 
      <td class="party "> 
      Lib. 
      </td> 
      <td class="votes "> 
      54,798 
      </td> 
      <td class="pct "> 
      1.1% 
      </td> 
      <td class="change "> 
      &ndash; 
      </td> 
      <td class="evotes last"> 
      0 
      </td> 
     </tr> 
     <tr class="last-row"> 
      <td class="photo first"> 

      </td> 
      <td class="candidate "> 
      div class="not-winner">Jill Stein</div> 
      </td> 
      <td class="party "> 
      Green 
      </td> 
      <td class="votes "> 
      29,336 
      </td> 
      <td class="pct "> 
      0.6% 
      </td> 
      <td class="change "> 
      &ndash; 
      </td> 
      <td class="evotes last"> 
      0 
      </td> 
     </tr> 
     <tr> 
     <td class="footer" colspan="100"> 
      <a href="/2012/results/president">President Map</a> &nbsp;|&nbsp; 
      <a href="/2012/results/president/big-board">President Big Board</a>&nbsp;|&nbsp; 
      <a href="/2012/results/president/exit-polls?state=il">Exit Polls</a> 
     </td> 
     </tr> 
    </tbody> 
</table> 

Problemi correlati