2014-12-31 4 views
0

특정 Google 검색에서 제공되는 모든 링크와 미리보기 이미지를 가져오고 싶습니다. 여기 내 코드가있다. Google 구문 분석 Google 결과

package com.esocial.util; 

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 ListLinks { 
    public static void main(String[] args) throws IOException { 
     String url = "https://www.google.co.in/webhp?sourceid=chrome-instant&rlz=1C1CHWA_enIN609IN609&ion=1&espv=2&ie=UTF-8#q=thermodynamics%20cbse"; 
     System.out.println("Fetching : "+url+"\n\n"); 

     Document doc = Jsoup.connect(url).userAgent("Mozilla").get(); 

     Elements div = doc.select("div.srg"); 

     for(Element di : div) 
     { 
      Elements lists = di.select("li.g"); 

      for(Element list : lists) 
      { 
       Element anc = list.select("a").first(); 
       Element img = list.select("img").first(); 


       System.out.println("\nLink : "+anc.attr("href")+"\nImage Link : "+img.attr("src")+"\n------------------------------------------\n"); 

      } 
     } 
    } 

} 

는하지만이 코드가 제대로 실행되지 않고 결과를 표시하지 않습니다. 나는 그 문제가 무엇인지 이해하지 못한다.

+1

정확히 추출하려고하는 대상은 무엇입니까? 또한'div.srg'는 무엇입니까 (나는 그런 클래스와 div를 찾을 수 없습니다)? 아마 당신이 그것을 분석하기 전에'doc'의 내용을 인쇄하는 것을 고려해보십시오. 또한 Jsoup는 JavaScript 에뮬레이터가 아니라 간단한 파서이므로 JavaScript로 생성 된 내용이 있으면 파싱 할 수 없습니다. – Pshemo

+0

다양한 div에 내장되어 있습니다. 이것들은 목록 안에있는 div이고, 그 목록 항목은 앵커 태그와 img 태그입니다. jsoup에서 코딩을 시작한 이래로이 잘못된 방식으로 접근하고있을 수 있습니다. 앵커 링크와 해당 img 링크를 가져 와서 링크를위한 CSV 파일을 만들면 좋을 것입니다. –

+0

'System.out.println (doc)'을 통해'doc'의 내용을 출력 할 때'srg' 클래스로'div'를 찾을 수 있습니까? – Pshemo

답변

0

나는 또한 검색 결과를 얻기 위해 작업했습니다.

html 페이지를 가져 오려면 나는 REST 후 호출을 요청했다.

// HTTP GET request 
private void sendGet(String query, GoogleSearchCallback googleSearchCallback) throws Exception { 
    ArrayList<GoogleSearchResult> googleSearchResults = new ArrayList<>(); 
    String url = "https://www.google.co.in/search?q=" + query + "#q=" + query + "course"; 

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

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

    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)"); 

    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 
    System.out.println(response.toString()); 
    Document document = Jsoup.parse(response.toString()); 
    Elements links = document.select("a[href]"); 
    for (Element link : links) { 

     String temp = link.attr("href"); 
     if (temp.startsWith("/url?q=")) { 
      Log.i(TAG, "" + temp.replace("/url?q=", "") + ""); 

      URL tempUrl = new URL(temp.replace("/url?q=", "")); 
      String path = tempUrl.getFile().substring(0, tempUrl.getFile().lastIndexOf('/')); 
      String base = tempUrl.getProtocol() + "://" + tempUrl.getHost() + path; 
      Log.i(TAG, "" + base); 
      Log.e(TAG, getDomainName(temp)); 

      GoogleSearchResult googleSearchResult = new GoogleSearchResult(); 
      googleSearchResult.setResultLink(base); 
      googleSearchResult.setResultTitle(link.text()); 
      googleSearchResults.add(googleSearchResult); 
     } 
    } 
    googleSearchCallback.onGetGoogleResultSuccess(googleSearchResults); 
} 

private String getDomainName(String url) { 

    String domainName = ""; 
    matcher = patternDomainName.matcher(url); 
    if (matcher.find()) { 
     domainName = matcher.group(0).toLowerCase().trim(); 
    } 
    return domainName; 

} 

private static Pattern patternDomainName; 
private Matcher matcher; 
private static final String DOMAIN_NAME_PATTERN = "^(http(s)?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; 

static { 
    patternDomainName = Pattern.compile(DOMAIN_NAME_PATTERN); 
} 
관련 문제