First of all, I looked for similar questions, but I couldn't find the answer I need. So, pardon me if this question is not unique and new.
I want to get the first N (probably 5 or 10) results of google as links. At the moment I have something like this:
String url="http://www.google.com/search?q=";
String charset="UTF-8";
String key="java";
String query = String.format("%s",URLEncoder.encode(key, charset));
URLConnection con = new URL(url+ query).openConnection();
//next line is to trick Google who is blocking the default UserAgent
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
This gives me the complete google html code of this searching, but I only want to get the raw links of the first n results. How do I manage that?
Thanks in advance.