1

我想从这个网站获得标题:http: //feeds.foxnews.com/foxnews/latest

像这个例子:

<title><![CDATA[SUCCESSFUL INTERCEPT Pentagon confirms it shot down ICBM-type target]]></title>

它将显示如下文本:

“成功拦截五角大楼确认击落洲际弹道导弹型目标美国成功进行导弹拦截试验,五角大楼说”

这是我的代码。我用过 jaunt 库。

我不知道为什么它只显示文字“foxnew.com”

import com.jaunt.JauntException;
import com.jaunt.UserAgent;

public class p8_1
{

    public static void main(String[] args)
    {
        try
        {
            UserAgent userAgent = new UserAgent();
            userAgent.visit("http://feeds.foxnews.com/foxnews/latest"); 
            String title = userAgent.doc.findFirst
("<title><![CDATA[SUCCESSFUL INTERCEPT Pentagon confirms it shot down ICBM-type target]]></title>").getText();
              System.out.println("\n " + title); 


        } catch (JauntException e)
        {
            System.err.println(e);
        }

    }

}
4

1 回答 1

0

搜索元素类型,而不是值。

尝试以下操作以获取提要中每个项目的标题文本:

public static void main(String[] args) {
    try {
        UserAgent userAgent = new UserAgent();
        userAgent.visit("http://feeds.foxnews.com/foxnews/latest");

        Elements items = userAgent.doc.findEach("<item>");
        Elements titles = items.findEach("<title>");

        for (Element title : titles) {
            String titleText = title.getComment(0).getText();
            System.out.println(titleText);
        }
    } catch (JauntException e) {
        System.err.println(e);
    }
}
于 2017-05-31T06:44:45.540 回答