2011-09-05 10 views
0

특정 클래스의 URL을 계산하고 싶습니다. 클래스는Python : 특정 클래스의 URL 계산

<h1 class="sectionTitle">INSIDERS AT LOEWS CORP (L)</h1> 

내가에만이 클래스 아래 링크의이 종류의 수를 계산하고 싶은

<a href="../../../research/stocks/people/relationship.asp?personId=228893&symbol=L:US"> 

같은 몇 가지 링크가된다. 이것은 내 프로그램이지만 "카운트"를 기록하면 작동하지 않습니다.

i = 0 
headings = bs.find('h1', text='INSIDERS AT LOEWS CORP (L)') 
for section2 in headings.findNext(''): 
    aa= section2.findAll('a', {'href': True}) 
    bb=aa.count('href') 
    print bb 
i = i + 1; 

작동하지 않습니다 ..... 문제를 해결하기 위해 팁을 주시겠습니까 ??? 정말 고마워요!

<h1 class="sectionTitle">INSIDERS AT LOEWS CORP (L)</h1> 
<table cellpadding="0" cellspacing="0" class="table" width="100%" style="margin-bottom:5px;"><thead><tr><td>Name (Connections)</td><td colspan="2" style="width:120px;">Board Relationships</td><td>Title</td><td>Type of Board Member</td><td align="right">Age</td></tr></thead><tr><td><a href="../../../research/stocks/people/person.asp?personId=228893&symbol=L:US" class="link_xsb">Andrew Tisch </a></td><td style="width:28px; padding-left: 5px;"><a href="../../../research/stocks/people/relationship.asp?personId=228893&symbol=L:US"><img src="../../images/icons/people2.gif" style="vertical-align:middle"/></a></td><td> <strong><a href="/businessweek/research/stocks/people/relationship.asp?personId=228893&symbol=L:US">53</strong> Relationships</a></td><td style="width:200px">Co-Chairman, Member of the Office of the President, Chairman of Executive Committee, Member of Finance Committee and Chairman of Bulova</td><td >--</td><td align="right" style="width:20px">61</td></tr><tr><td><a href="../../../research/stocks/people/person.asp?personId=285942&symbol=L:US" class="link_xsb">Jonathan Tisch </a></td><td style="width:28px; padding-left: 5px;"><a href="../../../research/stocks/people/relationship.asp?personId=285942&symbol=L:US"><img src="../../images/icons/people2.gif" style="vertical-align:middle"/></a></td><td> <strong><a href="/businessweek/research/stocks/people/relationship.asp?personId=285942&symbol=L:US">56</strong> Relationships</a></td><td style="width:200px">Co-Chairman, Member of the Office of the President, Member of Executive Committee, Chairman of Loews Hotels and Chief Executive Officer of Loews Hotels</td><td >--</td><td align="right" style="width:20px">57</td></tr><tr><td><a href="../../../research/stocks/people/person.asp?personId=285936&symbol=L:US" class="link_xsb">James Tisch </a></td><td style="width:28px; padding-left: 5px;"><a href="../../../research/stocks/people/relationship.asp?personId=285936&symbol=L:US"><img src="../../images/icons/people3.gif" style="vertical-align:middle"/></a></td><td> <strong><a href="/businessweek/research/stocks/people/relationship.asp?personId=285936&symbol=L:US">240</strong> Relationships</a></td><td style="width:200px">Chief Executive Officer, President, Member of Office of the President, Director, Member of Executive Committee, Member of Finance Committee, Chairman of Diamond Offshore and Director of CNA</td><td >--</td><td align="right" style="width:20px">58</td></tr></table> 
+0

나는 당신의'bs' 객체가 당신이 이것을 위해'Beautiful Soup'를 사용하고 있다고 가정합니다. 작동하지 않는다고 말할 때 문제가 무엇입니까? 오류가 발생 했습니까? 아니면 셀 수없는 항목을 찾지 못하고 있습니까? – mwan

+0

정말로'i = i + 1;'줄이없고 끝 부분에 세미콜론이 있습니까? 그 때문에 스크립트가 작동하지 않게됩니다. – mwan

+0

세미콜론으로 인해 스크립트가 작동하지 않습니다. 파이썬 코드에서 세미콜론을 사용하는 것은 합법적입니다. 당신이 그것을 사용해야하는 것은 아니지만. –

답변

1

내가 jQuery를 같은 강한 선택을 제공 PyQuery 추천 jQuery를의 큰 팬이기 때문에.

from pyquery import PyQuery as pq 
dom = pq(file('your.html').read()) 
print len(dom('h1.sectionTitle + table a')) 

h1은 요소이며. 클래스 이름입니다. 대상이 클래스가 아니라 id 인 경우 ID 이름에 #을 사용할 수 있습니다. +는 다음 인접 요소를 나타냅니다. 이 경우, 다음 테이블 요소. 여기에 테이블을 추가하여 테이블 안의 A 요소를 반환합니다.