GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Use Google Custom Search Engine API in Python - The Python Code Get a head start on your coding projects with our Python Code Generator. Perfect for those times when you need a quick solution. Don't wait, try it today! Google Custom Search Engine (CSE) is a search engine that is suited for developers in which it lets you include a search engine in your application, whether it is a website, a mobile app, or anything else. Since manually scraping Google Search is highly unsuggested, as it will restrict with a reCAPTCHA every few queries, in this tutorial, you will learn how you can set up a CSE and use its API in Python. In CSE, you can customize your engine that searches for results on specific websites, or you can use your website only. However, we will enable our search engine to search the entire web for this tutorial. Learn also: How to Use Google Drive API in Python. Setting Up a CSEFirst, you need to have a Google account to set up your search engine. After that, head to the CSE page and sign in to Custom Search Engine as shown in the following figure: After you log in to your Google account, a new panel will appear to you that looks something like this: You can include the websites you want to include in your search results, choose the language of your search engine, and set up its name. Once finished, you'll be redirected to this page: Using CSE API in PythonNow to use your Search Engine in Python, you need two things: First, you need to get your Search Engine ID, you can get easily find it in the CSE control panel:
Second, you have to generate a new API key, head to the Custom Search JSON API page, and click on the "Get a Key" button there, a new window will appear, you need to create a new project (you can name it whatever you want) and click on Next button, after that you'll have your API key, here is my result: Lastly, if you want to search the entire web, you need to enable it on your control panel:
Let's initialize our CSE requirements:
We will do a simple search query, "python" for demonstration purposes. Let's build the API URL we'll request:
Here we're building the URL that we will request. By default, the CSE API returns the first ten search results. Changing the Making the API request and using the requests' json() method to automatically parse the returned JSON data to a Python dictionary:
Now, this data is a dictionary containing many result tags. We are only interested in "items", which are the search results. By default, CSE will return ten search results. Let's iterate over them:
We are extracting the title, long description, the snippet (i.e., description), and the link to the resulting page. Check the result:
That's awesome, I have snipped the output to only three search results, but it will print ten results in your case. ConclusionYou can specify various request parameters to customize your search. You can also print the data dictionary to see other metadata, such as total results, search time, and even the meta tags of each page. Check CSE's documentation for further details. By default, it is limited to 100 search queries per day for free access. If you feel that isn't enough for your application, then consider signing up for billing in the API Console. A great use case of this API is to get the Google ranking of a specific page by a specific keyword. Check this tutorial in which I show you how! Now you can integrate searching techniques into your applications, and I hope this tutorial was helpful and easy to follow. If you like it, please don't hesitate to share it! There are other tutorials on Google APIs. Here are some of them if you want to check them out:
Read also: How to Make a URL Shortener in Python. Happy Coding ♥ Why juggle between languages when you can convert? Check out our Code Converter. Try it out today!
Sharing is caring!
Read alsoHow to Use Github API in PythonUsing Github Application Programming Interface v3 to search for repositories, users, making a commit, deleting a file, and more in Python using requests and PyGithub libraries. How to Use Google Drive API in PythonLearn how you can use Google Drive API to list files, search for specific files or file types, download and upload files from/to Google Drive in Python. How to Translate Languages in PythonLearn how to make a language translator and detector using Googletrans library (Google Translation API) for translating more than 100 languages with Python. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |