I m going to scrape every results in every pages with google search . I restricted the search time range to be :1/1/2016 to 31/12/2016
String string = google + URLEncoder.encode(search , charset) + news+"&tbs=cdr%3A1%2Ccd_min%3A1%2F1%2F2016%2Ccd_max%3A12%2F31%2F2016";
I find that this is not working out .
I m using JSoup.
Now , I successfully scrape every results in a specific number of pages .numberOfResultpages
But I want to scrape every results in every pages google could find (NOT in a specific number of pages)
Here is my work.
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
String[] line = new String[100];
final int[] score = { 0};
String google = "http://www.google.com/search?q=";
String search = "stackoverflow";
String charset = "UTF-8";
String news="&tbm=nws";
String string = google + URLEncoder.encode(search , charset) + news+"&tbs=cdr%3A1%2Ccd_min%3A1%2F1%2F2016%2Ccd_max%3A12%2F31%2F2016";
int numberOfResultpages = 10; // >==grabs specific number of pages only
int idx = 0;
for (int i = 0; i < numberOfResultpages; i++) {
Document document = Jsoup.connect(string).userAgent(userAgent) .data("start",""+i).get();
Elements links = document.select( ".r>a");
for (Element link : links) {
String title = link.text();
String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");
if (!url.startsWith("http")) {
continue; // Ads/news/etc.
}
System.out.println("Title: " + title);
System.out.println("URL: " + url);
line[idx++]=title;
// }
}
}