1

Calibre を使用して Web サイトのレシピを作成しています。

Web ソース コードは次のとおりです。

  <section>

  <h1 class="fly-title">Leaders</h1>

    <article>
  <h2><a href="/node/21537908" class="package-link">Democracy and its enemies</a></h2>

    <a href="/node/21537908"><img src="http://media.economist.com/sites/default/files/imagecache/news_package_primary_landscape/20120123_LDC001_0.gif" alt="" title=""  class="imagecache imagecache-news_package_primary_landscape" width="412" height="232" /></a>  
  <p>
    In the coming year the people who run the world will change—and so could the ideas, predicts John Micklethwait    <a href="/node/21537908/comments#comments" title="Comments" class="comment-icon"><span>(0)</span></a>  </p>

</article>  
    <ul class="package-item"><li class="first"><div class="">
  <a href="/node/21537909" class="package-link">The year of self-induced stagnation</a>  <a href="/node/21537909/comments#comments" title="Comments" class="comment-icon"><span>(7)</span></a></div>
</li>
<li class="even"><div class="">
  <a href="/node/21537914" class="package-link">How to run the euro?</a>  <a href="/node/21537914/comments#comments" title="Comments" class="comment-icon"><span>(2)</span></a></div>
</li>
<li class=""><div class="">
  <a href="/node/21537916" class="package-link">Wanted: a fantasy American president</a>  <a href="/node/21537916/comments#comments" title="Comments" class="comment-icon"><span>(0)</span></a></div>
</li>
<li class="even"><div class="">
  <a href="/node/21537917" class="package-link">Poking goes public</a>  <a href="/node/21537917/comments#comments" title="Comments" class="comment-icon"><span>(7)</span></a></div>
</li>
<li class=""><div class="">
  <a href="/node/21537918" class="package-link">Varied company</a>  <a href="/node/21537918/comments#comments" title="Comments" class="comment-icon"><span>(0)</span></a></div>
</li>
<li class="even"><div class="">
  <a href="/node/21537919" class="package-link">All eyes on London</a>  <a href="/node/21537919/comments#comments" title="Comments" class="comment-icon"><span>(0)</span></a></div>
</li>
<li class="last"><div class="">
  <a href="/node/21537921" class="package-link">And now for some non-events</a>  <a href="/node/21537921/comments#comments" title="Comments" class="comment-icon"><span>(2)</span></a></div>
</li>
</ul>  
  </section>

全部見つけたい<a href="/node/********" class="package-link">

綺麗なスープを使ったので

for section in soup.findAll('section'): 
...
    for post in section.findAll('a', attrs={'class':['package-link']})

しかし、見つかったのは最初の 1 つだけです (つまり、 の 1 つです<h2><a href="/node/21537908" class="package-link">Democracy and its enemies</a></h2>)。

それらをすべて見つけるにはどうすればよいですか?

4

1 に答える 1

0

私のために働く:

soup = BeautifulSoup.BeautifulSoup(xml)    
for section in soup.findAll("section"):
    for post in section.findAll('a', attrs={'class':['package-link']}):
        print post

結果:

<a href="/node/21537908" class="package-link">Democracy and its enemies</a>
<a href="/node/21537909" class="package-link">The year of self-induced stagnation</a>
<a href="/node/21537914" class="package-link">How to run the euro?</a>
<a href="/node/21537916" class="package-link">Wanted: a fantasy American president</a>
<a href="/node/21537917" class="package-link">Poking goes public</a>
<a href="/node/21537918" class="package-link">Varied company</a>
<a href="/node/21537919" class="package-link">All eyes on London</a>
<a href="/node/21537921" class="package-link">And now for some non-events</a>

編集

私が使用するバージョン:

  • Python 2.7.3
  • BeautifulSoup 3.2.0
于 2012-07-25T07:01:20.157 に答える