2017-09-20 8 views
0

articleタグの下にある最初のリンクだけを削り取ろうとしています。これまでのところ、私は記事のタグの下の両方のリンクをつかむこのpython web scrapingでarticleタグの下の最初のリンクを取得する

for link in soup.find("section", {"id": "grid"}).findAll("a", href=re.compile("/recipe/[0-9]*/.*/")): 
    if 'href' in link.attrs: 
     print(link.attrs['href']) 

が持っている

http://images.media-allrecipes.com/userphotos/250x250/00/17/17/171761.jpg 「">

<a href="/recipe/17066/janets-rich-banana-bread/" data-internal-referrer-link='hub recipe' data-click-id='cardslot 2' > 

    <img class="grid-col__rec-image" data-lazy-load data-original-src="http://images.media-allrecipes.com/userphotos/250x250/00/17/17/171761.jpg" alt="Janet's Rich Banana Bread Recipe and Video - Sour cream guarantees a moist and tender loaf. And bananas are sliced instead of mashed in this recipe, giving a concentrated banana taste in every bite." title="Janet's Rich Banana Bread Recipe and Video" src="http://images.media-allrecipes.com/ar/spacer.gif" style="display: inline;" /> 

    <h3 class="grid-col__h3 grid-col__h3--recipe-grid"> 
     Janet's Rich Banana Bread 
      <div class="grid-col__video"> 
       <a href="/video/1027/janets-rich-banana-bread/" data-internal-referrer-link='hub recipe' data-click-id='cardslot 2'><span class="icon--videoplay-small-white"></span></a> 
      </div> 
    </h3> 
</a> 
<a href="/recipe/17066/janets-rich-banana-bread/" data-internal-referrer-link='hub recipe' data-click-id='cardslot 2'> 
    <div class="grid-col__ratings"> 
     <div class="rating-stars" data-scroll-to-anchor="reviews" data-ratingstars= 4.82000017166138 > 
    <img height="16" width="16" src="http://images.media-allrecipes.com/ar-images/icons/rating-stars/full-star-2015.svg" /> 
    <img height="16" width="16" src="http://images.media-allrecipes.com/ar-images/icons/rating-stars/full-star-2015.svg" /> 
    <img height="16" width="16" src="http://images.media-allrecipes.com/ar-images/icons/rating-stars/full-star-2015.svg" /> 
    <img height="16" width="16" src="http://images.media-allrecipes.com/ar-images/icons/rating-stars/full-star-2015.svg" /> 
    <img height="16" width="16" src="http://images.media-allrecipes.com/ar-images/icons/rating-stars/full-star-2015.svg" /> 

 <div class="rec-card__description">Sour cream guarantees a moist and tender loaf. And bananas are sliced instead of mashed in this recipe, giving a concentrated banana taste in every bite.</div> 

    </div> 
</a> 
<div class="profile profile--recipe-card"> 
      <a href="/cook/8133/" data-internal-referrer-link='hub recipe' data-click-id='cardslot 2'> 
       <ul class="cook-details"> 
        <li> 
         <img class="img-profile elevate-cook-thumbnail" alt="profile image" src="http://images.media-allrecipes.com/mobile/allrecipes/images/icon-user-default_v2.png"/> 
        </li> 
        <li> 
           <h4><span>Recipe by</span> vjonsson</h4> 
         <ul class="cook-details__followers followers-count"> 
          <li> 
           <span class="icon--cook-card-follower" title="Followers"></span> 
           <span></span> 
          </li> 
          <li> 
           <span><format-large-number number="21"></format-large-number></span> 
          </li> 
         </ul> 
         <ul class="cook-details__favorites favorites-count"> 
          <li> 
           <span class="icon--cook-card-favorite" title="Favorites"></span> 
           <span></span> 
          </li> 
          <li> 
           <span><format-large-number number="0"></format-large-number></span> 
          </li> 
         </ul> 
         <ul class="cook-details__recipes-made recipes-made-count"> 
          <li> 
           <span class="icon--cook-card-made" title="Recipes Made"></span> 
           <span></span> 
          </li> 
          <li> 
           <span><format-large-number number="3"></format-large-number></span> 
          </li> 
         </ul> 
        </li> 
       </ul> 
      </a> 
</div> 

ご覧のとおり、2つのリンクがあり、最初のものだけを取得しようとしています。どんな助けもありがとう!

答えて

関連する問題