0

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

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
4

1 回答 1

0

在您的代码中,iter.next()返回Element实例。然后将Element实例与字符串连接起来,这样toString()就会调用该方法。

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

于 2013-05-27T08:00:42.700 回答