java - easiest (legal) way to programmatically get the google search result count? -
i want estimated result count google search engine queries (on whole web) using java code.
i need few queries per day, @ first google web search api, though deprecated, seemed enough (see e.g. how can search google programmatically java api). turned out, numbers returned api different returned www.google.com (see e.g. http://code.google.com/p/google-ajax-apis/issues/detail?id=32). these numbers pretty useless me.
i tried google custom search engine, exhibits same problem.
what think simplest solution task?
/**** @author rajesh kharche */ //open netbeans //choose java->prject //name googlesearchapp package googlesearchapp; import java.io.*; import java.net.*; import java.util.*; import java.util.logging.level; import java.util.logging.logger; public class googlesearchapp { public static void main(string[] args) { try { // todo code application logic here final int result; scanner s1=new scanner(system.in); string str; system.out.println("enter query search: ");//get query search str=s1.next(); result=getresultscount(str); system.out.println("results:"+ result); } catch (ioexception ex) { logger.getlogger(googlesearchapp.class.getname()).log(level.severe, null, ex); } } private static int getresultscount(final string query) throws ioexception { final url url; url = new url("https://www.google.com/search?q=" + urlencoder.encode(query, "utf-8")); final urlconnection connection = url.openconnection(); connection.setconnecttimeout(60000); connection.setreadtimeout(60000); connection.addrequestproperty("user-agent", "google chrome/36");//put browser name/version final scanner reader = new scanner(connection.getinputstream(), "utf-8"); //scanning buffer object returned http request while(reader.hasnextline()){ //for each line in buffer final string line = reader.nextline(); if(!line.contains("\"resultstats\">"))//line line scanning "resultstats" field because want extract number after continue; try{ return integer.parseint(line.split("\"resultstats\">")[1].split("<")[0].replaceall("[^\\d]", ""));//finally extract number convert string integer }finally{ reader.close(); } } reader.close(); return 0; } }
Comments
Post a Comment