Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting more than 10 results by Google Custom Search API V1 in Java

I am using Google Custom Search API in Java to get results of Google in response to a query. I have written this code with the help of other posts, code is as follows:

    url = new URL("https://www.googleapis.com/customsearch/v1?key="+key+ "&cx="+ cx +"&q="+    searchText+"&alt=json"+"&start="+0+"&num="+30);
    HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
    System.out.println("Connection opened!");
    conn2.setRequestMethod("GET");
    conn2.setRequestProperty("Accept", "application/json");
    BufferedReader br = new BufferedReader(new InputStreamReader(
    (conn2.getInputStream())));

The problem is that whenever I am using the above code without num and start parameters it is executing properly, but giving only top 10 results. So I have used num and start parameters. But they are creating problems. Here I cannot understand where to put the num and start parameters in the url. It is always giving HTTP 400 i.e. Bad Request. I have read the Documentation page, there also no clear instruction is given about where to put these two parameters in Url.

So if anyone helps me to solve this problem I will be really grateful. Thank you.

like image 673
Joy Avatar asked Jun 04 '13 19:06

Joy


People also ask

How do I get more than 10 results on Google API?

IF YOU ARE USING THE API Google Custom Search and Google Site Search return up to 10 results per query. If you want to display more than 10 results to the user, you can issue multiple requests (using the start=0, start=11 … parameters) and display the results on a single page.

Is there an API for Google Search results?

While scraping is not allowed as per their terms of use, Google does provide an alternative and legitimate way of capturing search results. If you hear yourself ask, “Is there a Google Search API?” the answer is a resounding YES.

How to get Google Custom Search results from Google API for free?

#Laravel Google Custom Search Engine Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version. As Swiftype closed free plans, I started to find some alternative without too much coding, but was unsucessfull.

What is Google Custom search JSON API?

The Custom Search JSON API lets you develop websites and applications to retrieve and display search results from Google Custom Search programmatically. With this API, you can use RESTful requests to get either web search or image search results in JSON format. Custom Search JSON API can return results in JSON data format.

How many websites can I search with the API?

If you need more than 10k queries per day and your Programmable Search Engine searches 10 sites or fewer, you may be interested in the Custom Search Site Restricted JSON API, which does not have a daily query limit. (the only catch with this is that you can search only Upton 10 websites).

How do I find my Google Custom Search Engine id?

If you have not already created a Google custom search engine, you can start by visiting the Google custom search engine control panel. After you have created a Google custom search engine, visit the instruction towards the end of this blog to learn how to locate your Search engine ID. Custom Search JSON API requires the use of an API key.


Video Answer


1 Answers

You can't do it that way. num can only be a maximum of 10. See

https://developers.google.com/custom-search/json-api/v1/reference/cse/list#num

num - unsigned integer
Number of search results to return. Valid values are integers between 1 and 10, inclusive.

To show more results, Google suggests making multiple calls, incrementing the start parameter as needed:

https://developers.google.com/custom-search/json-api/v1/reference/cse/list#start

start - unsigned integer The index of the first result to return. Valid value are integers starting 1 (default) and the second result is 2 and so forth. For example &start=11 gives the second page of results with the default "num" value of 10 results per page. Note: No more than 100 results will ever be returned for any query with JSON API, even if more than 100 documents match the query, so setting (start + num) to more than 100 will produce an error. Note that the maximum value for num is 10.

like image 200
Bumptious Q Bangwhistle Avatar answered Sep 29 '22 13:09

Bumptious Q Bangwhistle