1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   |  
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
 
 
public class Test
{
    public static void main(String[] args)
    {
        String language = "fr";
        String keywords = "toto";
        HttpClient httpclient = new DefaultHttpClient();
 
        try
        {
            HttpGet httpget = new HttpGet("http://www.google.com/search?hl="+language+"&q=" +keywords);
            System.out.println("Execute la requete " + httpget.getURI());
 
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
 
            Pattern pattern = Pattern.compile("<a href=[^>]*class=l");
            Matcher matcher = pattern.matcher(responseBody);
 
            while(matcher.find())
            {
                String links = matcher.group();
                System.out.println(links.substring(16, links.length()-9)); 
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            httpclient.getConnectionManager().shutdown();    
        }
    }
 
} | 
Partager