2014-12-03 54 views
1

我需要在我的android應用程序中刮取一個url。該URL的回報HTML代碼如下塊:Android中的Scrape Html

<div id="main"> 
    <div id="header"> 
    <form action="/search_db.php" id="f1" method="GET"> 
    <div style="float:left; width:829px;"> 
    <span style="margin:15px;"><a href="http://mp3skull.com/"><img src="http://mp3skull.com/img/logo.jpg" border="0" alt="mp3skull.com - mp3 downloads" style="vertical-align:middle;" /></a></span> 
    <input type="text" name="q" id="sfrm" autocomplete="off" value="feel good inc gorillaz" style="font-size:18px; vertical-align:middle; width:470px;"> 
    <input type="hidden" name="fckh" value="c1935e9a779034dec31fe7117c456eb8"> 
    <input type="submit" id="search_button" value="Search" style="font-size:18px; vertical-align:middle;"> 
    </div> 
    <div style="float:left; text-align:right;"> 
    </div> 
    <div style="clear:both;"></div> 
    </form><script type="text/javascript">document.getElementById('sfrm').focus();InstallAC(document.getElementById('f1'), document.getElementById('sfrm'), document.getElementById('search_button'), '', 'en');</script> 
</div> 

請告訴我如何提取的java

回答

2

返回的HTML代碼的價值使用jsoup一個例子。

Document doc = Jsoup.connect("http://your/url/here").get(); // or Jsoup.parse(htmlString); 
Elements header = doc.select("#header"); //access to <div id="header">...</div> 
    Elements inputs = header.select("input"); 
    for(Element input : inputs){ 
     System.out.println(input); //print <input>....</input> 
     System.out.println(input.attr("id")); //printing attribute id 
    }