0

我使用 getElementBytag 方法从以下 XML 文档中提取数据(雅虎财经新闻 api http://finance.yahoo.com/rss/topfinstories


我使用以下代码。它使用 getelementsBytag 方法获取新项目和标题没有问题,但由于某种原因,在按标签搜索时不会拾取链接。它只获取链接元素的结束标签。是 XML 文档的问题还是 jsoup 的问题?

import java.io.IOException;         
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;   

class GetNewsXML {
    /**
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String args[]){
        Document doc = null;
        String con = "http://finance.yahoo.com/rss/topfinstories";
        try {
            doc = Jsoup.connect(con).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Elements collection = doc.getElementsByTag("item");// Gets each news item
        for (Element c: collection){
            System.out.println(c.getElementsByTag("title"));
        }
        for (Element c: collection){
            System.out.println(c.getElementsByTag("link"));
        }
    }
4

1 回答 1

1

你得到<link /> http://...; 链接作为文本节点放在-tag之后link

但这不是问题:

final String url = "http://finance.yahoo.com/rss/topfinstories";

Document doc = Jsoup.connect(url).get();


for( Element item : doc.select("item") )
{
    final String title = item.select("title").first().text();
    final String description = item.select("description").first().text();
    final String link = item.select("link").first().nextSibling().toString();

    System.out.println(title);
    System.out.println(description);
    System.out.println(link);
    System.out.println("");
}

解释:

item.select("link")  // Select the 'link' element of the item
    .first()         // Retrieve the first Element found (since there's only one)
    .nextSibling()   // Get the next Sibling after the one found; its the TextNode with the real URL
    .toString()      // Get it as a String

通过您的链接,此示例打印所有元素,如下所示:

Tax Day Freebies and Deals
You made it through tax season. Reward yourself by taking advantage of some special deals on April 15.
http://us.rd.yahoo.com/finance/news/rss/story/SIG=14eetvku9/*http%3A//us.rd.yahoo.com/finance/news/topfinstories/SIG=12btdp321/*http%3A//finance.yahoo.com/news/tax-day-freebies-and-deals-133544366.html?l=1

(...)
于 2013-04-14T18:01:31.230 回答