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>

我想打印:

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

我能够按照建议使用它 - Jsoup getting a hyperlink from li

( doc.selectdiv div divn li ui ...) 并获得班级反馈。

我应该如何使用 select 命令来获取上述值的值?

4

3 回答 3

2

要获取属性的值,请使用attr方法。例如

Elements elements = doc.select("meta");
for(Element e: elements)
  System.out.println(e.attr("content"));
于 2012-09-16T09:02:59.463 回答
0


在一次选择中...您是否尝试过逗号组合符“,”? 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 个元素,即日期和选择后的反馈。

于 2015-03-07T00:44:03.467 回答
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);
于 2015-04-24T04:07:37.697 回答