2013-01-03 97 views
1

我的Android應用程序將從電話號碼獲取運營商信息。我打算使用Jsoup(或另一個Java HTML解析器)來抓取表中顯示的載體信息。如何從使用Java(Android)的網站上抓取數據?

我試圖從fonefinder.net

查詢URL格式刮是:

http://www.fonefinder.net/findome.php?npa=**first 3 digits**&nxx=**next 3 digits**&thoublock=**final 4 digits** 

頁面的HTML是一個簡單的表格(見下文)。我想從第2行,第5列,其中一個鏈接出現在格式

http://fonefinder.net/(CARRIER_NAME).php 

其中CARRIER_NAME是像「Verizon公司」的值提取數據。我需要幫助瞭解如何提取這些數據。

<table border="3" cellspacing="2" cellpadding="2" bgcolor="#FFFFCC"> 
    <tbody> 
    <tr bgcolor="#7093DB" align="CENTER"> 
     <th> 
     Area Code 
     </th> 
     <th>Prefix</th> 
     <th> 
     City/Switch Name 
     <br> 
     (Click for city search) 
     </th> 
     <th> 
     State/Prov. 
     <br> 
     Area Map 
     </th> 
     <th> 
     Telephone Company 
     <br/> 
     Web link 
     </th> 
     <th> 
     Telco 
     <br/> 
     Type 
     </th> 
     <th> 
     Map/Zip 
     <br/> 
     Detail 
     </th> 
    </tr> 
    <tr> 
     <td> 
     **first 3 digits** 
     </td> 
     <td> 
     **next 3 digits** 
     </td> 
     <td> 
     City Name 
     </td> 
     <td> 
     State Name 
     </td> 
     <td> 
     <a href="http://fonefinder.net/CARRIER_NAME.PHP">carrier name</a> 
     </td> 
     <td>WIRELESS PROV</td> 
     <td> 
     map 
     </td> 
    </tr> 
    </tbody> 
</table> 
+1

幾個小時前你沒有發佈同樣的問題嗎?!? –

+0

如果我編寫了一個2行java代碼,它可以讓你獲得這些信息,而不是使用任何第三方分析器,它會好嗎? – Deepak

回答

0

我寫的代碼大量使用Jsoup的選擇語法通過名稱解析的標籤,但你也可以通過CSS類,ID屬性等等解析。 Jsoup selector syntax documentation有您可以使用的選擇器的完整列表。

import java.io.IOException; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class StackOverflowQuestion { 

    public static void main(String[] args) { 

     try { 
      // get the tables on this page, note I masked the phone number 
      Document doc = Jsoup.connect("http://www.fonefinder.net/findome.php?npa=###&nxx=###&thoublock=####").get(); 
      Elements tables = doc.select("table"); 

      // get the second table, 0 indexed 
      Element secondTable = tables.get(1); 

      // get the columns 
      Elements tds = secondTable.select("td"); 

      //get the 5th column, 0 indexed 
      Element fifthTd = tds.get(4); 

      //get the link in this column 
      Element link = fifthTd.select("a").first(); 

      //print out the URL 
      System.out.println(link.attr("href")); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}