2012-08-08 7 views
1

Google 웹 이미지에서 반환 한 결과를 크롤링하고 싶습니다. Google에서 제공하는 도구가 있습니까? 다양한 주제에 대한 교육 사례가 필요한 객체 인식 시스템을 구축하고 있습니다.Google 웹 이미지를 크롤링하는 방법

+0

, 당신이 사용되지 않습니다 것을 볼 수 있습니다. –

답변

2

구글이 자신의 검색 API되지 않는 것처럼이, 당신에게 도움이 될 수 있습니다

구글 맞춤 검색 웹 사이트 또는 웹 사이트의 수집을 통해 검색 할 수 있습니다. 귀하의 필요와 관심 분야에 맞는 검색 엔진을 생성하고 귀하의 웹 사이트에 검색 결과를 표시하려면 Google의 힘을 이용하십시오. 사용자 지정 검색 엔진은 지정한 웹 사이트를 기반으로 검색 결과에 우선 순위를 부여하거나 제한 할 수 있습니다 ( ).

https://developers.google.com/custom-search/

2

이에 대한 구글의 이미지 API를 사용할 수 있습니다.
예 :

$url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=stackoverflow"; 

// sendRequest 
// note how referer is set manually 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */); 
$body = curl_exec($ch); 
curl_close($ch); 

// now, process the JSON string 
$json = json_decode($body); 
// now have some fun with the results... 

더 많은 정보 : 당신이 이미지 API 페이지의 상단으로 스크롤하면 https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php

+0

해당 페이지의 맨 위로 스크롤하면 더 이상 사용되지 않습니다. –

+0

더 이상 사용되지 않는 것은 아닙니다 ... 나를 위해, 코드는 매우 잘 작동합니다. – Julien

+0

"Google 이미지 검색 API는 2011 년 5 월 26 일부터 공식적으로 지원 중단 될 예정입니다. Google의 지원 중단 정책에 따라 계속 작동하지만 하루에 요청할 수있는 요청 수가 제한 될 수 있습니다. 이제 이미지 검색을 지원하는 맞춤 검색 API로 업그레이드하는 것이 좋습니다. " - Google https://developers.google.com/image-search/ –

0
package GoogleImageDownload; 

import java.io.* 
import java.net.HttpURLConnection; 
import java.net.URL; 

import javax.net.ssl.HttpsURLConnection; 
import org.w3c.dom.* 

public class HttpURLConnectionExample { 

private final String USER_AGENT = "Chrome/44.0.2403.157"; 

public static void main(String[] args) throws Exception { 

    HttpURLConnectionExample http = new HttpURLConnectionExample(); 

    System.out.println("Testing 1 - Send Http GET request"); 
        String url = "https://www.google.co.in/search?tbm=isch&q=test"; 

    http.sendGet(url); 

    System.out.println("\nTesting 2 - Send Http POST request"); 
    //http.sendPost(); 

} 

// HTTP GET request 
private void sendGet(String url) throws Exception { 


    URL obj = new URL(url); 
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

    // optional default is GET 
    con.setRequestMethod("GET"); 

    //add request header 
    con.setRequestProperty("User-Agent", USER_AGENT); 

    int responseCode = con.getResponseCode(); 
    System.out.println("\nSending 'GET' request to URL : " + url); 
    System.out.println("Response Code : " + responseCode); 

    BufferedReader in = new BufferedReader(
      new InputStreamReader(con.getInputStream())); 
    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 
    in.close(); 

     //print result 
     String Html2Xml = light_html2xml.Html2Xml(response.toString()); 
     Document convertStringToDocument = DocumentObjectClass.convertStringToDocument(Html2Xml); 
     NodeList Images = convertStringToDocument.getElementsByTagName("img"); 
     for(int i = 0;i<Images.getLength();i++) 
     { 
      Node node= Images.item(i); 
      if (node.getNodeType() == Node.ELEMENT_NODE) 
      { Element elem = (Element) node; 

       if(Integer.parseInt(elem.getAttribute("height").replace("px", ""))>10&&Integer.parseInt(elem.getAttribute("width").replace("px", ""))>10) 
       { 
        System.out.println(elem.getAttribute("src")); 
        try{ 
        saveImage(elem.getAttribute("src"),String.valueOf(i)); 
        } 
        catch(Exception e){System.err.println(e.getMessage());} 

       } 
      } 
     } 
        NodeList href = convertStringToDocument.getElementsByTagName("a"); 
     for(int i = 0;i<href.getLength();i++) 
     { 
      Node node= href.item(i); 
      if (node.getNodeType() == Node.ELEMENT_NODE) 
      { Element elem = (Element) node; 

       if(elem.getAttribute("href")!=null) 
       { 

        try{ 
         sendGet(elem.getAttribute("href"));       } 
        catch(Exception e){System.err.println(e.getMessage());} 

       } 
      } 
     }    

} 


public static void saveImage(String imageUrl,String name) throws IOException { 
URL url = new URL(imageUrl); 
String fileName = url.getFile(); 

String destName = new File(".").getAbsolutePath()+"/"+name+".jpg"; 
System.out.println(destName); 

    OutputStream os; 
     try (InputStream is = url.openStream()) { 
      os = new FileOutputStream(destName); 
      byte[] b = new byte[2048]; 
      int length; 
      while ((length = is.read(b)) != -1) { 
       os.write(b, 0, length); 
} } 
os.close(); 
} 
} 
관련 문제