2012-12-23 3 views
1

많은 HTML 태그를 반복하고 있지만 약간의 지침이 필요합니다. Beautiful Soup로 테이블 행을 반복하고 링크 텍스트를 출력하려면 어떻게해야합니까?BeautifulSoup을 사용하여 HTML 구문 분석

HTML은 :

<div id="thisDiv"> 
    <table class="thisTable"> 
     <tr> 
      <td><a href="blablabl">BLABLABLA1</a></td><td><a href="blablabla2">BLABLABLA2</a></td> 
     </tr> 
     <tr> 
      <td><a href="blablab3">BLABLABLA3</a></td><td><a href="blablabla4">BLABLABLA4</a></td> 
     </tr> 
     <tr> 
      <td><a href="blablab5">BLABLABLA5</a></td><td><a href="blablabla6">BLABLABLA6</a></td> 
     </tr> 
    </table> 
</div> 
+0

(http://whathaveyoutried.com) –

답변

1

당신은 행을 반복 할 필요가 없습니다. 그냥 테이블을 찾아 포함 된 모든 앵커 검색 :

[? 당신이 시도 무엇]
soup = BeautifulSoup(doc) 
table = soup.find('table', {'class':'thisTable'}) 
for a in table.findAll('a'): 
    print a.text 
+0

감사합니다. 클래스 thisTable에 여러 개의 테이블이있는 경우 #thisDiv의 하위 테이블을 어떻게 지정합니까? – KingKongFrog

+0

@KingKongFrog는'div = soup.find ('div', { 'id': 'thisDiv'})'를 사용하고 하나의 테이블 만 포함하는 경우 div 자체에서 앵커를 검색 할 수 있습니다. 그렇지 않으면'table = div.find (...) '로 div 내의 테이블을 찾습니다. –

관련 문제