2013-01-03 2 views
1

내 Android 앱은 전화 번호에서 이동 통신사 정보를 가져옵니다. Jsoup (또는 다른 Java HTML 파서)를 사용하여 표에 표시된 이동 통신사 정보를 다룰 예정입니다. 내가 fonefinder.netJava (Android)를 사용하여 웹 사이트에서 데이터를 긁는 방법은 무엇입니까?

에서 쿼리 URL 형식을 긁어 시도하고

은 다음 페이지의

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

html로 간단한 테이블 (아래 참조). 나는 링크가 CARRIER_NAME는 "버라이존"와 같은 값이 형식

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

에 나타나는 2 행 5 열에서 데이터를 추출하려합니다. 이 데이터를 추출하는 방법을 찾는 데 도움이 필요합니다.

<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

제 3 자 파서를 사용하는 대신에이 정보를 얻는 2 행 자바 코드를 작성하면 괜찮습니까? – 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(); 
     } 
    } 
} 
관련 문제