2012-09-16 67 views
0
<div class="Class-feedbacks"> 
    <div class="grading class2"> 
    <div itemtype="http://xx.edu/grading" itemscope="" itemprop="studentgrading"> 
     <div class="rating"> 
     <img class="passportphoto" width="1500" height="20" src="http://greg.png" > 
     <meta content="4.0" itemprop="gradingvalue"> 
     </div> 
    </div> 
    <meta content="2012-09-08" itemprop="gradePublished"> 
    <span class="date smaller">9/8/2012</span> 
    </div> 
    <p class="review_comment feedback" itemprop="description">Greg is one the smart person in his batch</p> 
</div> 

我想打印:的GetValue(JSoup)

date: 2012-09-08 
Feedback : Greg is one the smart person in his batch 

我能夠用這個作爲建議 - Jsoup getting a hyperlink from li

doc.select(DIV DIV DIVN李UI ......)和得到課堂反饋。

我該如何使用select命令獲取上述值的值?

回答

2

要獲取屬性的值,請使用attr方法。例如。

Elements elements = doc.select("meta"); 
for(Element e: elements) 
    System.out.println(e.attr("content")); 
+0

謝謝Nicolas。我的是一個複雜的網頁,有這麼多div。所以我使用選擇器作爲= doc.select(「div div div div div div div.Class-feedbacks)。所以我怎樣才能得到反饋和日期?你解釋我的邏輯是得到一個信息?我需要兩個..pl糾正我 –

+0

對於反饋元素,您可以使用'doc.select(「div div div div div div div.Class-feedbacks p.feedback」)。text()'。 – Nicholas

+0

其實對於這個I必須做的選擇?有沒有辦法我可以得到更好的使用一個選擇..當我最初嘗試的時候是這樣的:我必須寫這兩個這些數組(2數組)我打印從array1 = date和arry2 = feedback。所以我一直在尋找如何讓這個選擇,以便我保存一些處理 –

0


在一個單一的選擇......你試圖逗號Combinator的 「」? http://jsoup.org/apidocs/org/jsoup/select/Selector.html

Elements elmts = doc.select("div.Class-feedbacks meta, p") 

Element elmtDate = elmts.get(0); 
System.out.println("date: " + elmtDate.attr("content")); 
Element elmtParag = elmts.get(1); 
System.out.println("Feedback: " + elmtParag.text()); 

你應該在你的列表後面2個元素的日期和之後的選擇反饋。

0

這是一個老問題,我可能會遲到,但如果其他人想知道如何輕鬆做到這一點,下面的代碼將會有所幫助。

Document doc = Jsoup.parse(html); 
// We select the meta tag whose itemprop property has value 'gradePublished' 
String date = doc.select("meta[itemprop=gradePublished]").attr("content"); 
System.out.println("date: "+date); 
// Now we select the text inside the p tag with itemprop value 'description' 
String feedback = doc.select("p[itemprop=description]").text(); 
System.out.println("Feedback: "+feedback);