2016-03-12 57 views
1
<th "data-next="/?operator=comcast&from=hbo#guide" > 
<a href="/hbo/" title="HBO"> 
    <div> 
    <img src="//comcast.com/channel_logo/hbo.png?0"> 
    </div> 
    <span>HBO</span> 
</a> 
</th> 
<th "data-next="/?operator=att&from=fox#guide" > 
<a href="/fox/" title="fox"> 
    <div> 
    <img src="//att.com/channel_logo/fox.png?0"> 
    </div> 
    <span>FOX</span> 
</a> 
</th> 

我想獲得每一個環節就是在data-next,所以我想有: /?operator=comcast&from=hbo#guide/?operator=att&from=fox#guide。但我有一個解釋問題,因爲我不知道data-next是什麼。這不是一個屬性,或一個元素,所以我不知道我應該用什麼jsoup。我感謝所有幫助jsoup得到表頭的元素

編輯:

整個表頭看起來是這樣的:

<thead class="channelLogos"> 
<tr> 
    <th "data-next="/?operator=comcast&from=hbo#guide"> <a href="/hbo/" title="HBO"> 
    <div> 
    <img src="//comcast.com/channel_logo/hbo.png?0"> 
    </div> <span>HBO</span> </a> </th>(...) 

當我做了這樣的:

Elements elts = doc.select("thead.channelLogos th") 
for(Element elt : elts) { 
    System.out.println(elt.absUrl("data-next")); 
}//elts stores th elements but doesn't print anything 

但是這樣的:

Elements elts = doc.select("thead.logaStacji th[data-next]"); 

elts是空的(大小= 0

+0

有'數據next'前一個雙引號。這就是爲什麼沒有打印。嘗試刪除這個錯誤的字符。 – Stephan

+0

但這正是它在這個網頁的源代碼中的樣子,所以它可能是一個錯誤。在這種情況下,我應該使用'replace()'''substring()'來自己修復它嗎? – user3529850

+0

是的,你應該。 – Stephan

回答

1

試試這個:

String html = loadHTML(...); 

Document doc = Jsoup.parse(html); 

Elements elts = doc.select("th[data-next]"); 

for(Element elt : elts) { 
    // Get absolute url stored in data-next attribute 
    System.out.println(elt.absUrl("data-next")); 
} 
+0

我已經對我的帖子提出了一個答案,似乎我做錯了什麼。 – user3529850