2013-05-23 4 views
0

현재 Scrapers 용 코드를 작성하고 있으며 점점 더 파이썬의 팬이되고 특히 BeautifulSoup가됩니다.BeautifulSoup - Python에서 동일한 태그를 건너 뛰는 방법

아직도 ... html을 통해 구문 분석 할 때 나는 그렇게 아름다운 방법으로 만 사용할 수있는 어려운 부분을 발견했습니다.

나는 HTML 코드 특히 다음 코드 긁어하려면 :

<div class="title-box"> 
    <h2> 
     <span class="result-desc"> 
      Search results <strong>1</strong>-<strong>10</strong> out of <strong>10,009</strong> about <strong>paul mccartney</strong><a href="alert/settings" class="title-email-alert-promo x-title-alerts-promo">Create email Alert</a> 
     </span> 
    </h2> 
</div> 

을 그래서 내가 사용하여 DIV를 식별하는 것입니다 무엇을 :.

comment = TopsySoup.find('div', attrs={'class' : 'title-box'}) 

그런 추악한 부분에 제공을하는 방법 내가 가지고 할 번호를 잡을 : 10009를 내가 사용 : 더 좋은 방법이 있는지

catcher = comment.strong.next.next.next.next.next.next.next 

누군가가 말해 줄래?

답변

3

어때 대략 comment.find_all('strong')[2].text?

는 이는 실제로 함수 그것에 find_all 전화와 동일하다 비록 같은 Tag 객체를 호출하기 때문에, comment('strong')[2].text으로 단축 할 수있다.

>>> comment('strong')[2].text 
u'10,009' 
관련 문제