작은 웹 크롤러를 개발하려고합니다.이 웹 크롤러는 웹 페이지를 다운로드하고 특정 섹션의 링크를 검색합니다. 하지만이 코드를 실행하면 "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;}
}
검색을 수행하려면 [Amazon API] (https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html)를 방문해야한다고 생각합니다. –
방금 예제로 사용했습니다 ... 사용할 수있는 웹 사이트. 그것의 학습 목적을 위해. – user2696466
웹 스크래핑에 대해 배우고 싶다면 [this] (http://www.onelook.com/)과 같은 사이트를 사용하여 결과를 구문 분석하는 것이 훨씬 쉽다는 점을 고려하여 시작하는 것이 좋습니다. –