2013-06-01 3 views
-1

웹 사이트에서 페이지 소스를 가져 와서 코드 스 니펫 만 저장하는 프로그램을 작성하려고합니다.스캐너를 사용한 특정 데이터 마이닝

package Program; 

import java.net.*; 
import java.util.*; 

public class Program { 
public static void main(String[] args) { 
    String site = "http://www.amazon.co.uk/gp/product/B00BE4OUBG/ref=s9_ri_gw_g63_ir01?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-5&pf_rd_r=0GJRXWMKNC5559M5W2GB&pf_rd_t=101&pf_rd_p=394918607&pf_rd_i=468294"; 
    try { 
     URL url = new URL(site); 
     URLConnection connection = url.openConnection(); 
     connection.connect(); 
     Scanner in = new Scanner(connection.getInputStream()); 
     while (in.hasNextLine()) { 
      System.out.println(in.nextLine()); 
     } 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 
} 

지금까지 출력에 코드 만 표시됩니다. 프로그램에서 특정 문자열을 검색하고 가격 만 표시하고 싶습니다. 예 :

<tr id="actualPriceRow"> 
<td id="actualPriceLabel" class="priceBlockLabelPrice">Price:</td> 
<td id="actualPriceContent"><span id="actualPriceValue"><b class="priceLarge">£599.99</b></span> 
<span id="actualPriceExtraMessaging"> 

검색 class="priceLarge"> 만 표시/저장 내가 모든 솔루션을 환영하지만,하지만 난 정말 어떤 PHP를 이해하지 않고 자바 솔루션을하고자하는 웹 사이트에 비슷한 질문이 있다는 것을 알고 599.99

에 대한 :)

+1

귀하의 가격을 찾기 위해 무엇을 시도했다 : 대답은 여기 lpful 및입니까? 무슨 문제가 있었 니? –

+5

정규 표현식으로이 작업을 수행 할 수는 있지만 실제로 xml/html 구문 분석 라이브러리를 사용해야합니다. 학습은 웹 프로그래밍에 관심이 있다면 앞으로 많은 노력을 절약 할 것입니다. – greedybuddha

+2

이것은 HTML이기 때문에 jsoup를 사용하면 더 쉬울 것입니다. – fge

답변

0

예를 들어 일부 라이브러리를 구문 분석에 사용할 수 있습니다. Jsoup

Document document = Jsoup.connect("http://www.amazon.co.uk/gp/product/B00BE4OUBG/ref=s9_ri_gw_g63_ir01?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-5&pf_rd_r=0GJRXWMKNC5559M5W2GB&pf_rd_t=101&pf_rd_p=394918607&pf_rd_i=468294").get(); 

당신은 콘크리트 요소

Elements el = document.select("b.priceLarge"); 

을 검색 할 수 있습니다 그리고 당신은 영업 이익이 질문 편집에 쓴

String content = el.val(); 
0

처럼이 요소의 내용을 얻을 수 있습니다 :

답장을 보내 주셔서 감사합니다.

package Project; 
import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

public class Project { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 

    Document doc; 
    try { 
     doc = Jsoup.connect("url of link").get(); 
     String title = doc.title(); 
     System.out.println("title : " + title); 
     String pricing = doc.getElementsByClass("priceLarge").text(); 
     String str = pricing; 
     str = str.substring(1); 
     System.out.println("price : " + str); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 
} 
+0

(질문 [대답]에서 편집하여 커뮤니티 위키로 변환] (http://meta.stackoverflow.com/questions/267434/what-is-the-appropriate-action-when-the-answer-to-a- 질문에 추가됨) –

관련 문제