繁体   English   中英

如何使用Jaunt库从网站上抓取数据?

[英]How can I scrape data from a website using the Jaunt library?

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

像这样的例子:

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

它会显示如下文字:

五角大楼说:“成功截获五角大楼确认击落ICBM型目标美国进行成功的导弹拦截试验”

这是我的代码。 我用过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);
        }

    }

}

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

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

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM