march alex's blog
hello,I am march alex
posts - 52,comments - 7,trackbacks - 0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

import com.google.gson.Gson;



public class GoogleFinder {
    
    
public static void main(String[] args) throws IOException, InterruptedException {
        
        String address 
= "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
        String query 
= "";
        String charset 
= "UTF-8";
        
        Scanner in 
= new Scanner(System.in);
        String targeturl 
= in.nextLine();
        query 
= in.nextLine();
        
        
for(int i=0;i<125;i++) {
            
            URL url 
= new URL(address + URLEncoder.encode(query, charset) + "&start=" + i);
            
            
//System.out.println(url);
            Reader reader = new InputStreamReader(url.openStream(), charset);
            GoogleResults results 
= new Gson().fromJson(reader, GoogleResults.class);
            
            
for(int t=0;t<30;t++) {
                System.out.print(
".");
                Thread.sleep(
2000);
            } System.out.println(
"");
            
            
for(int j=0;j<4;j++) {

                String ss 
= results.getResponseData().getResults().get(j).getUrl().toLowerCase();
                
if(ss.contains(targeturl)) {
                    System.out.println(ss);
                    System.out.println(results.getResponseData().getResults().get(j).getTitle());
                    System.out.println(
"Found in the " + (i*4+j+1+ " th");
                    
return;
                }
            }
            System.out.println(
"" + ((i+1)*4+ " pages found.");
        }
        
        System.out.println(
"Can find " + targeturl + " in the first 500 pages");
        
    }
}


class GoogleResults {

    
private ResponseData responseData;

    
public ResponseData getResponseData() {
        
return responseData;
    }

    
public void setResponseData(ResponseData responseData) {
        
this.responseData = responseData;
    }

    
public String toString() {
        
return "ResponseData[" + responseData + "]";
    }

    
static class ResponseData {
        
private List<Result> results;

        
public List<Result> getResults() {
            
return results;
        }

        
public void setResults(List<Result> results) {
            
this.results = results;
        }

        
public String toString() {
            
return "Results[" + results + "]";
        }
    }

    
static class Result {
        
private String url;
        
private String title;

        
public String getUrl() {
            
return url;
        }

        
public String getTitle() {
            
return title;
        }

        
public void setUrl(String url) {
            
this.url = url;
        }

        
public void setTitle(String title) {
            
this.title = title;
        }

        
public String toString() {
            
return "Result[url:" + url + ",title:" + title + "]";
        }
    }
}

posted on 2015-07-29 14:16 marchalex 阅读(135) 评论(0)  编辑  收藏 所属分类: java小程序

只有注册用户登录后才能发表评论。


网站导航: