import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GoogleFinderNew {
private static String address = "https://www.google.com.hk/search?hl=en&q=";
private static String query = "";
private static String charset = "UTF-8";
private static void test() {
String ans = "abc<h3>helloworld</h3>hehe<h3>nicetomeetyou</h3>";
int st = ans.indexOf("<h3>");
int ed = ans.indexOf("</h3>");
System.out.println(st + " " + ed);
System.out.println(ans.substring(st+4, ed));
}
private static List<String> getpages(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
String ans = "";
while ((line = reader.readLine()) != null){
ans += line + "\n";
}
int st = -1, ed = 0;
List<String> pagesList = new ArrayList<String>();
while((st = ans.indexOf("<h3 class=\"r\"><a href=\"", ed)) != -1) {
ed = ans.indexOf("\"", st+23);
//System.out.println(ans.substring(st+23, ed));
pagesList.add(ans.substring(st+23, ed));
}
return pagesList;
}
public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
Scanner in = new Scanner(System.in);
String askurl = in.nextLine();
query = in.nextLine();
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11");
int pages = 100;
for(int i=0;i<pages;i++) {
System.out.println((i+1) + " ..");
String urlString = address + URLEncoder.encode(query, charset);
if(i != 0) urlString += "&start=" + i + "0";
//System.out.println(urlString);
List<String> list = getpages(new URL(urlString));
for(String page : list) {
if(page.contains(askurl)) {
// if(page.equals(askurl)) {
System.out.println(askurl + " found in the " + (i+1) + " th page.");
System.out.println(page);
return;
}
}
Thread.sleep(1000);
}
System.out.println("can't find " + askurl + " in the first " + pages + " pages.");
}
}
posted on 2015-07-29 14:16
marchalex 阅读(301)
评论(0) 编辑 收藏 所属分类:
java小程序