2014-10-22 45 views
1

我想製作一個動態網站,並需要一些網絡圖片。我決定把它們從flickr上刮掉,然後把我的網站上的所有者包括進來,但我遇到了一些問題。我會在下面發佈HTML的一部分,但如果你想自己檢查源代碼,這裏是網站。 https://www.flickr.com/exploreJSoup通過屬性值抓取HTML文檔

HTML:

<div class="thumb ">    

    <span class="photo_container pc_ju"> 
     <a data-track="photo-click" href="/photos/sheilarogers13/15586482942/in/explore-2014-10-20" title="Lake District" class="rapidnofollow photo-click"><img id="photo_img_15586482942" src="https://c2.staticflickr.com/4/3945/15586482942_6a7154363f_z.jpg"width="508" height="339" alt="Lake District" class="pc_img " border="0"><div class="play"></div></a> 
    </span> 
    <div class="meta"> 
     <div class="title"><a data-track="photo-click" href="/photos/sheilarogers13/15586482942/in/explore-2014-10-20" title="Lake District" class="title">Lake District</a></div> 

     <div class="attribution-block"> 
      <span class="attribution"> 
       <span>by </span> 
       ******<a data-track="owner" href="/photos/sheilarogers13" title="sheilarogers22" class="owner">sheilarogers22</a>****** 
      </span> 
     </div> 

     <span class="inline-icons"> 

       <a data-track="favorite" href="#" class="rapidnofollow fave-star-inline canfave" title="Add this photo to your favorites?"><img width="12" height="12" alt="[★]" src="https://s.yimg.com/pw/images/spaceball.gif" class="img"><span class="fave-count count">99+</span></a> 
      <a title="Comments" href="#" class="rapidnofollow comments-icon comments-inline-btn"> 
       <img width="12" height="12" alt="Comments" src="https://s.yimg.com/pw/images/spaceball.gif"> 
       <span class="comment-count count">57</span> 
      </a> 
      <a href="#" data-track="lightbox" class="rapidnofollow lightbox-inline" title="View in light box"><img width="12" height="12" alt="" src="https://s.yimg.com/pw/images/spaceball.gif"></a> 
     </span> 
    </div>  
</div> 

我想去的地方,我把星號,爲了能夠給予信貸圖片的作者就行了。

我的代碼:

Elements pgElem = doc.select("div.thumb").select("div.meta").select("[data-track]"); 

上面的代碼但是讓我在我的div.meta所有4個數據磁道,雖然,我只想要=所有者之一。

我檢查了JSoup文檔,它說帶有值的屬性是使用[attr=value]找到的,但我似乎無法讓它工作。我已經試過:

.select("[data-track=owner]")

.select("[data-track='owner']")

但既不工作。思考?

回答

4
 Elements pgElem = doc.select("div.thumb").select("div.meta").select("[data-track]"); 
     Elements ownerElements = new Elements(); 
     for(Element element:pgElem){ 
      if(!element.getElementsByAttributeValueContaining("data-track","owner").isEmpty()){ 
       ownerElements.add(element); 
      } 
     } 

其實,我只是給它的另一個旋轉,這對我的作品:

doc.select("div.thumb").select("div.meta").select("[data-track=owner]") 
+0

感謝。任何想法爲什麼文檔說 「[attr = value]:具有屬性值的元素,例如[width = 500](也可引用,如序列)」 這是否爲非字符串屬性? source:http: /jsoup.org/cookbook/extracting-data/selector-syntax – Tiberiu 2014-10-22 02:19:45