2013-08-19 7 views
0

작은 웹 크롤러를 개발하려고합니다.이 웹 크롤러는 웹 페이지를 다운로드하고 특정 섹션의 링크를 검색합니다. 하지만이 코드를 실행하면 "href"태그의 링크가 짧아지고 있습니다. 같은 :java의 웹 크롤러. 웹 페이지 문제 다운로드

원래 링크 : "/ 어린이 - 장난감 액션 피규어 - 액세서리/B/REF = toys_hp_catblock_actnfigs 즉 = UTF8 & 노드 = 165,993,011 & pf_rd_m = ATVPDKIKX0DER & pf_rd_s = 상품화 검색-4 & pf_rd_r = 267646F4BB25430BAD0D & pf_rd_t = 101 & pf_rd_p = 1,582,921,042 & pf_rd_i = 165793011 "

가로 전환 :"/ 어린이 - 장난감 액션 피규어 - 액세서리/B 즉 = UTF8 & 노드 = 165993011 "

은 아무도 나를 도울 수 부디. 이 서버 앱이 데스크톱 브라우저와 자바 기반 크롤러의 요청에 다르게 반응하는 상황처럼 나에게 느낌

package test; 
import java.io.*; 
import java.net.MalformedURLException; 
import java.util.*; 
public class myFirstWebCrawler { 

public static void main(String[] args) { 

    String strTemp = ""; 
    String dir="d:/files/"; 
    String filename="hello.txt"; 
    String fullname=dir+filename; 

    try { 
     URL my_url = new URL("http://www.amazon.com/s/ref=lp_165993011_ex_n_1?rh=n%3A165793011&bbn=165793011&ie=UTF8&qid=1376550433"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream(),"utf-8")); 
     createdir(dir); 
     while(null != (strTemp = br.readLine())){ 
      writetofile(fullname,strTemp); 
     System.out.println(strTemp); 

     } 
     System.out.println("index of feature category : " + readfromfile(fullname,"Featured Categories")); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

} 


public static void createdir(String dirname) 
{ File d= new File(dirname); 

    d.mkdirs(); 


} 

public static void writetofile(String path, String bbyte) 
{ 
    try 
    { 
     FileWriter filewriter = new FileWriter(path,true); 
     BufferedWriter bufferedWriter = new BufferedWriter(filewriter); 
     bufferedWriter.write(bbyte); 
     bufferedWriter.newLine(); 
     bufferedWriter.close(); 
    } 
    catch(IOException e) 
    {System.out.println("Error");} 

} 

public static int readfromfile(String path, String key) 
{ 
    String dir="d:/files/"; 
    String filename="hello1.txt"; 
    String fullname=dir+filename; 
    linksAndAt[] linksat=new linksAndAt[10]; 
    BufferedReader bf = null; 
    try { 
     bf = new BufferedReader(new FileReader(path)); 
    } catch (FileNotFoundException e1) { 

     e1.printStackTrace(); 
    } 
    String currentLine; 
    int index =-1; 
    try{ 
     Runtime.getRuntime().exec("cls"); 
    while((currentLine = bf.readLine()) != null) 
    { 
     index=currentLine.indexOf(key); 
     if(index>0) 
     { 
      writetofile(fullname,currentLine); 
      int count=0; 
      int lastIndex=0; 
      while(lastIndex != -1) 
      { 
       lastIndex=currentLine.indexOf("href=\"",lastIndex); 

       if(lastIndex != -1) 
       { 
        lastIndex+="href=\"".length(); 
        StringBuilder sb = new StringBuilder(); 
       while(currentLine.charAt(lastIndex) != '\"') 
        { 
         sb.append(Character.toString(currentLine.charAt(lastIndex))); 
         lastIndex++; 

        } 

        count++; 

        System.out.println(sb); 
       } 
      } 
      System.out.println("\n count : " + count); 
      return index; 
     } 

    } 
    } 
    catch(FileNotFoundException f) 
    { 
     f.printStackTrace(); 
    System.out.println("Error"); 
    } 
    catch(IOException e) 
    {try { 
     bf.close(); 
    } catch (IOException e1) { 
    e1.printStackTrace(); 
    }} 
    return index;} 
} 
+1

검색을 수행하려면 [Amazon API] (https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html)를 방문해야한다고 생각합니다. –

+0

방금 ​​예제로 사용했습니다 ... 사용할 수있는 웹 사이트. 그것의 학습 목적을 위해. – user2696466

+0

웹 스크래핑에 대해 배우고 싶다면 [this] (http://www.onelook.com/)과 같은 사이트를 사용하여 결과를 구문 분석하는 것이 훨씬 쉽다는 점을 고려하여 시작하는 것이 좋습니다. –

답변

0

: 아래에있는 내 코드입니다. 브라우저가 Java 기반 크롤러가 아닌 쿠키 (예 : 세션 유지 쿠키)를 전달하거나 브라우저가 사용자 크롤러와 다른 사용자 에이전트 헤더를 전달했기 때문일 수 있습니다. 데스크톱 브라우저와 Java 크롤러간에 다른 요청 헤더가 다른 경우 일 수 있습니다.

크롤링 앱을 작성할 때 이것은 가장 큰 문제 중 하나입니다. 즉, 다른 클라이언트가 요청한 동일한 URL이 항상 동일한 코드로 응답하지 않는다는 것을 잊기 쉽습니다. 그게 당신에게 일어난 일인지 모르겠지만, 매우 일반적입니다.