2014-03-06 111 views
0

我有一張表格,用於提取鏈接和文本。雖然我只能做一個或另一個。任何想法如何獲得這兩個?從BeautifulSoup Python獲取表格的值

基本上我需要拉文: 「TEXT TO解壓到」

for tr in rows: 
        cols = tr.findAll('td') 
        count = len(cols) 
        if len(cols) >1: 

         third_column = tr.findAll('td')[2].contents 
         third_column_text = str(third_column) 
         third_columnSoup = BeautifulSoup(third_column_text) 

#issue starts here. How can I get either the text of the elm <td>text here</td> or the href text<a href="somewhere.html">text here</a> 
         for elm in third_columnSoup.findAll("a"): 
          #print elm.text, third_columnSoup 
          item = { "code": random.upper(), 
             "name": elm.text } 
          items.insert(item) 

的HTML代碼如下

<table cellpadding="2" cellspacing="0" id="ListResults"> 
    <tbody> 
     <tr class="even"> 
      <td colspan="4">sort results: <a href= 
      "/~/search/af.aspx?some=LOL&amp;Category=All&amp;Page=0&amp;string=&amp;s=a" 
      rel="nofollow" title= 
      "sort results in alphabetical order">alphabetical</a>&nbsp;&nbsp;|&nbsp;&nbsp;<strong>rank</strong>&nbsp;&nbsp;<a href="/as.asp#Rank">?</a></td> 
     </tr> 

     <tr class="even"> 
      <th>aaa</th> 

      <th>vvv.</th> 

      <th>gdfgd</th> 

      <td></td> 
     </tr> 

     <tr class="odd"> 
      <td align="right" width="32">******</td> 

      <td nowrap width="60"><a href="/aaa.html" title= 
      "More info and direct link for this meaning...">AAA</a></td> 

      <td>TEXT TO EXTRACT HERE</td> 

      <td width="24"></td> 
     </tr> 

     <tr class="even"> 
      <td align="right" width="32">******</td> 

      <td nowrap width="60"><a href="/someLink.html" 
      title="More info and direct link for this meaning...">AAA</a></td> 

      <td><a href= 
      "http://www.fdssfdfdsa.com/aaa">TEXT TO EXTRACT HERE</a></td> 

      <td width="24"> 
       <a href= 
       "/~/search/google.aspx?q=lhfjl&amp;f=a&amp;cx=partner-pub-2259206618774155:1712475319&amp;cof=FORID:10&amp;ie=UTF-8"><img border="0" 
       height="21" src="/~/st/i/find2.gif" width="21"></a> 
      </td> 
     </tr> 

     <tr> 
      <td width="24"></td> 
     </tr> 

     <tr> 
      <td align="center" colspan="4" style="padding-top:6pt"> 
      <b>Note:</b> We have 5575 other definitions for <strong><a href= 
      "http://www.ddfsadfsa.com/aaa.html">aaa</a></strong> in our 
      database</td> 
     </tr> 
    </tbody> 
</table> 
+0

你能後整個HTML(或有關部分)你解析? – alecxe

+0

hey @alecxe我已經更新了代碼。許多Tks。 – user2091936

+0

這可能會幫助你! http://stackoverflow.com/questions/1817184/beautifulsoup-get-value-in-table –

回答

1

您可以只使用text屬性td元素:

from bs4 import BeautifulSoup 

html = """HERE GOES THE HTML""" 

soup = BeautifulSoup(html, 'html.parser') 
for tr in soup.find_all('tr'): 
    columns = tr.find_all('td') 
    if len(columns) > 2: 
     print columns[2].text 

打印:

TEXT TO EXTRACT HERE 
TEXT TO EXTRACT HERE 

希望有幫助。

+0

打印時出現錯誤third_column.text AttributeError:'list'object has no attribute'text' – user2091936

+0

其實,如果我做下面的工作third_column_text = str(third_column) third_columnSoup = BeautifulSoup(third_column_text) if third_columnSoup: print third_columnSoup.text – user2091936

+0

@ user2091936,我編輯了答案,重新檢查它。 – alecxe

0

做到這一點的方式是通過執行以下操作:

third_column = tr.find_all( 'TD')[2] .contents

   third_column_text = str(third_column) 
       third_columnSoup = BeautifulSoup(third_column_text) 
       if third_columnSoup: 
        print third_columnSoup.text