2014-03-06 2 views
0

나는 링크와 텍스트를 추출하는 테이블을 가지고 있습니다. 비록 내가 할 수있는 하나 또는 다른. 둘 다 얻는 방법에 대한 생각? HTML 코드는BeautifulSoup Python을 사용하여 테이블에서 값을 가져옵니다.

<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 (또는 관련을 게시 할 수 .contents 부분) 당신은 구문 분석 무엇입니까? – alecxe

+0

안녕하세요 @alecxe 코드를 업데이트했습니다. 많은 Tks. – user2091936

+0

도움이 될 수 있습니다! http://stackoverflow.com/questions/1817184/beautifulsoup-get-value-in-table –

답변

1

당신은 단지 td 요소에 text 속성을 사용할 수 있습니다 다음은 "여기에 압축을 해제하는 TEXT"

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) 

:

는 기본적으로 나는 텍스트를 끌어해야합니다

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'객체에 'text'속성이 없습니다. – user2091936

+0

실제로 third_columnSoup 경우가 third_column_text = str을 작동 다음 (third_column) third_columnSoup = BeautifulSoup로 (third_column_text)을 수행 – user2091936

+0

user2091936 @, 내가 대답을 편집 한 third_columnSoup.text 인쇄, 그것을 다시 확인. – alecxe

0

그것을 할 방법은 수행하여 않습니다 :

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

   third_column_text = str(third_column) 
       third_columnSoup = BeautifulSoup(third_column_text) 
       if third_columnSoup: 
        print third_columnSoup.text 
관련 문제