文本在Jsoup中无法正常显示

时间:2013-05-27 07:14:49

标签: java html jsoup

我想打印网页的用户评论以及他们给出的评分,我尝试了下面的代码

Elements links = doc.select("p.s_desc,span.s_rating_overal");
    //Elements links1 = doc.select();
    //System.out.println(links.next());
    ListIterator iter= links.listIterator();
    while(iter.hasNext())
    {
    //String test= iter.next().text();
    System.out.println("\n"+iter.next());
    System.out.println(iter.next()+"\n\n");


    }

问题是输出随标签一起出现。

<span class="s_rating_overal">5.3</span>
<p class="s_desc">You don't need a load of explanation on why this phone changed
 the market and set the standard, though the ties to AT&amp;T and Apple are way
 too strong to truly enjoy this device. &nbsp;The screen and style are now a bit
 outdated and the lack of customization make you feel like you are a cow in the t
 he big heard of i ecosystem. &nbsp;The ui is still probably the best there is bu
   t, come on its time for a face lift.</p>

如何只获取标签之间的文本 提前谢谢

如果我使用iter.next()。text();,我会收到以下错误

Crawler.java:44: cannot find symbol
symbol  : method text()
location: class java.lang.Object
            System.out.println("\n"+iter.next().text());
                                             ^
Crawler.java:45: cannot find symbol
symbol  : method text()
location: class java.lang.Object
            System.out.println(iter.next().text()+"\n\n");
                                        ^
2 errors

1 个答案:

答案 0 :(得分:0)

在您的代码中,iter.next()返回Element个实例。然后,您将Element实例与String连接,以便调用toString()方法。

toString()类的Element方法会将标记与内容一起返回。 如果您只想要元素的内容,则应使用text()方法。