您的选择器的问题是您使用的是ancestor child
选择器而不是.class
或element.class
喜欢div.star-box
。请注意,要使用多个类,您需要使用element.class1.class2
,或者.class1.class2
如果您不想指定element
.
另外,如果您想指定parent
child
关系,则必须使用>
,因此请尝试类似
Document doc = Jsoup.connect("http://www.imdb.com/title/tt0800369/").get();
Element rating = doc
.select("div.star-box.giga-star > div.titlePageSprite.star-box-giga-star")
.first();
System.out.println(rating);
Unfortunately this will print
<div class="titlePageSprite star-box-giga-star">
7.0
</div>
so if you want to get only text contend from that element use System.out.println(rating.text());
BTW since there is only one element with class star-box-giga-star
you can just use
String rating = doc.select(".star-box-giga-star").text();
as shown in Alex answer