2013-06-26 53 views
0

我想用jsoup解析這個HTML關於用jsoup解析Html的問題

我的代碼是:

doc = Jsoup.connect(htmlUrl).timeout(1000 * 1000).get(); 

      Elements items = doc.select("item"); 
      Log.d(TAG, "Items size : " + items.size()); 
      for (Element item : items) { 
       Log.d(TAG, "in for loop of items"); 

       Element titleElement = item.select("title").first(); 
       mTitle = titleElement.text().toString(); 
       Log.d(TAG, "title is : " + mTitle); 

       Element linkElement = item.select("link").first(); 
       mLink = linkElement.text().toString(); 
       Log.d(TAG, "link is : " + mLink); 

       Element descElement = item.select("description").first(); 
       mDesc = descElement.text().toString(); 
       Log.d(TAG, "description is : " + mDesc); 


      } 

我得到以下輸出:

in for loop of items 
D/HtmlParser(6690): title is : Indonesian president: Some multinationals "take too much" 
D/HtmlParser(6690): link is : 
D/HtmlParser(6690): description is : April 23 - Indonesian President Susilo Bambang Yudhoyono tells a Thomson Reuters Newsmaker event that the country welcomes foreign investment in its resources sector, but must receive a "fair share" of benefits.<div class="feedflare"> <a href="http://feeds.reuters.com/~ff/reuters/audio/newsmakerus/rss/mp3?a=NX3AY96GfGk:hAtGeOq2ESs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/reuters/audio/newsmakerus/rss/mp3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.reuters.com/~ff/reuters/audio/newsmakerus/rss/mp3?a=NX3AY96GfGk:hAtGeOq2ESs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/reuters/audio/newsmakerus/rss/mp3?i=NX3AY96GfGk:hAtGeOq2ESs:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.reuters.com/~ff/reuters/audio/newsmakerus/rss/mp3?a=NX3AY96GfGk:hAtGeOq2ESs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/reuters/audio/newsmakerus/rss/mp3?i=NX3AY96GfGk:hAtGeOq2ESs:F7zBnMyn0Lo" border="0"></img></a> </div><img src="http://feeds.feedburner.com/~r/reuters/audio/newsmakerus/rss/mp3/~4/NX3AY96GfGk" height="1" width="1"/> 

但我想作爲輸出:

in for loop of items 
D/HtmlParser(6690): title is : Indonesian president: Some multinationals "take too much" 
D/HtmlParser(6690): link is : http://feeds.reuters.com/~r/reuters/audio/newsmakerus/rss/mp3/~3/KDcQe4gF-3U/62828262.mp3 
D/HtmlParser(6690): description is : April 23 - Indonesian President Susilo Bambang Yudhoyono tells a Thomson Reuters Newsmaker event that the country welcomes foreign investment in its resources sector, but must receive a "fair share" of benefits. 

我應該在我的代碼改變?

如何實現我的目標。請幫幫我!!

預先感謝您!

回答

0

您提取的rss內容有2個問題。

  1. link文本不在<link/>標記內但在其外部。
  2. description標籤中有一些escaped html內容。

PFB修改後的代碼。

而且我發現了一些乾淨的HTML內容看在BrowserURL,這解析的時候都會讓你方便地提取所需的字段時。您可以在Jsoup中將userAgent設置爲Browser。但它取決於你決定如何獲取內容。

doc = Jsoup.connect("http://feeds.reuters.com/reuters/audio/newsmakerus/rss/mp3/").timeout(0).get(); 
    System.out.println(doc.html()); 
    System.out.println("================================"); 
    Elements items = doc.select("item"); 
    for (Element item : items) { 

     Element titleElement = item.select("title").first(); 
     String mTitle = titleElement.text(); 
     System.out.println("title is : " + mTitle); 

     /* 
     * The link in the rss is as follows 
     * <link />http://feeds.reuters.com/~r/reuters/audio/newsmakerus/rss/mp3/~3/NX3AY96GfGk/59621707.mp3 
     * which doesn't fall in the <link> element but falls under <item> TextNode 
     */ 
     String mLink = item.ownText(); // 
     System.out.println("link is : " + mLink); 

     Element descElement = item.select("description").first(); 
     /*Unescape the html content, Parse it to a doc, and then fetch only the text leaving behind all the html tags in content 
     * "/" is a dummy baseURI passed, as we don't care about resolving the links within parsed content. 
     */ 
     String mDesc = Parser.parse(Parser.unescapeEntities(descElement.text(), false),"/").text(); 
     System.out.println("description is : " + mDesc); 

    } 
+0

哇!很好地工作...... !!!謝謝!! 1 – Dhasneem

+0

可以請你看看這個鏈接http://stackoverflow.com/questions/17316659/unable-to-get-image-url-from-rss-feed-using-jsoup – Dhasneem