2012-11-06 17 views
10

सुंदर सूप प्रलेखन विशेषताओं .contents और किसी दिए गए टैग (एक सूची और क्रमश: एक iterable) के बच्चों तक पहुँचने के लिए .children प्रदान करता है से बच्चों कि टैग (नहीं नौगम्य स्ट्रिंग्स) कर रहे हैं मिलता है, और दोनों नौगम्य स्ट्रिंग्स शामिल और टैग। मैं प्रकार टैग की केवल बच्चों चाहते हैं।सुंदर सूप एक टैग

मैं वर्तमान में इस सूची समझ का उपयोग कर पूरा कर रहा हूँ:

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

लेकिन अगर वहाँ एक बेहतर/अधिक pythonic/निर्मित रास्ता सिर्फ बच्चों को टैग करने के लिए मैं सोच रहा हूँ।

+5

'table.tbody.find_all (यह सच है, पुनरावर्ती = झूठी)' (मैं इसे करने की कोशिश नहीं की है) – jfs

+0

[आप अपने खुद के पोस्ट कर सकता जवाब देने के] (http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/): इनपुट/आउटपुट के साथ डॉक्स के लिए लिंक, runnable कोड उदाहरण में शामिल हैं – jfs

उत्तर

14
J.F.Sebastian को

धन्यवाद, निम्नलिखित काम करेगा:

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

यहाँ प्रलेखन: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#true

मेरे मामले में, मैं तालिका में वास्तविक पंक्तियों की जरूरत है, तो मैं, निम्नलिखित का उपयोग कर रहा है जो समाप्त हो गया फिर

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

, डॉक्स: अधिक सटीक और मैं अधिक पठनीय लगता http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-using-tag-names

मेरा मानना ​​है कि यह एक टैग के सभी बच्चों के माध्यम से पुनरावृत्ति तुलना में एक बेहतर तरीका है।

निम्नलिखित इनपुट के साथ काम किया:

<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> 

संबंधित मुद्दे