How to use Google Custom Search Engine (GCSE) in your Python code

Learn to use Google search API in your code

Lynn Kwong
4 min readApr 30, 2021

--

Google is a great source for obtaining data, either in your personal projects or in real business projects. Google has a Custom Search Engine (CSE) API which can be conveniently used in your Python code. It has a free version which is enough for studying purposes or even small projects which have less than 100 search queries every day.

Photo by Benjamin Dada on Unsplash.

To get started using Google Custom Search Engine, you need to have a valid API key. You need to log in with your Google account and create a project for it. You can create a project on the fly or create one in the Google Cloud Platform Console. A project is used to organize all your Google Cloud resources. All your API keys, virtual machines, storage, etc. reside in some projects. The projects and API keys are free to create. However, the resources in the projects and the usage of the API keys can incur costs. Therefore, keep your API keys secrete and never expose them in a public repository.

Then you need to create a Search Engine ID. Go to the Programmable Search Control Panel, click the “Add” button under “Edit search engines”, then enter the fields required, as shown below. Importantly, you can add some sites where your custom search engine will search for data. You can add multiple sites if needed. In the advanced settings, you can also specify which sites should be excluded from searching. In this demo, we just want to search at https://www.w3schools.com/.

Once the search engine is created, go to “Control Panel” to check the advanced settings. In practical usage, you would normally want to fine-tune the settings, especially the “Sites to search” and “Restrict Pages using Schema.org Types”. Check the relevant sections for a detailed introduction to them.

For a demo of the search results with your custom search engine, you can open the “Public URL” in the control panel, or…

--

--

Lynn Kwong

I’m a Software Developer (https://superdataminer.com) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.