Google Custom Search Programmatically
Using JSON/Atom Custom Search API, you can use RESTful requests to get either web search or image search results in JSON or Atom format.
Pricing options for Google Custom Search
JSON/Atom Custom Search API pricing and quotas depend on the engine’s edition:
- Custom Search Engine (free): For CSE users, the API provides 100 search queries per day for free. If you need more, you may sign up for billing in the Cloud Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day.
- Google Site Search (paid): For detailed information on GSS usage limits and quotas, please check GSS pricing options.
Prerequisites for Google Custom Search
For performing google search through a program, you will need a developer api key and a custom search engine id.
How to get Google Developers API key?
Step 1: Log in to Google Developers Console with your google id (gmail).
Step 2: Create a project. Click on create project.
Step 3: Enter Project Name. Check the agreement check box and click on create.
Step 4: Now you can see the Project dashboard of newly created project. Now click on APIs & auth ->auth. Switch on the Custom Search API, by default it is off.
Step 5: Click on Credentials -> Create New Key (under Public api access)
Step 6: Click on Server key.
Step 7: Save your API key in notepad.
How to create Google Custom Search Engine?
Google Custom Search by Google allows web developers to include google search box in their web pages. The search box can be configured to search their own website or any other website they are configured to search.
Step 1: Log in to Google Custom Search
Step 2: Select New Search Engine
Step 3: Now you can enter the websites to be searched using google custom search. You can also include entire domains instead of specific websites as shown below.
Step 4: Click on create. It will show you below Screen.
Step 5: Click on get Code, note down the code (cx=xxxxx). If you want Google search box in you blog or website, you can copy paste the below script directly to your website.
Step 6: Your URL will look like below with search string “BasicsBehind”
Google Search Programmatically using JAVA
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class CustomGoogleSearch { final static String apiKey = "AIzaSyAFmFdHiFK783aSsdbq3lWQDL7uOSbnD-QnCnGbY"; final static String customSearchEngineKey = "00070362344324199532843:wkrTYvnft8ma"; final static String searchURL = "https://www.googleapis.com/customsearch/v1?"; public static String search(String pUrl) { try { URL url = new URL(pUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuffer buffer = new StringBuffer(); while ((line = br.readLine()) != null) { buffer.append(line); } return buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } private static String buildSearchString(String searchString, int start, int numOfResults) { String toSearch = searchURL + "key=" + apiKey + "&cx=" + customSearchEngineKey + "&q="; // replace spaces in the search query with + String newSearchString = searchString.replace(" ", "%20"); toSearch += newSearchString; // specify response format as json toSearch += "&alt=json"; // specify starting result number toSearch += "&start=" + start; // specify the number of results you need from the starting position toSearch += "&num=" + numOfResults; System.out.println("Seacrh URL: " + toSearch); return toSearch; } public static void main(String[] args) throws Exception { String url = buildSearchString("BasicsBehind", 1, 10); String result = search(url); System.out.println(result); } }
You can add other parameters to Google search with ‘&’ operator, for example lr=lang_en restricts the search to documents written in a English language.
You can find complete list of parameters here:-
Parameters to pass with the search query to filter the results:
https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
Language and country codes to pass with query:
https://developers.google.com/custom-search/docs/xml_results#countryCodes