Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In
Written By:
iamabhishek
6 minute read
PythonHowToGoogle

How to Perform Google Search using Python

Posted in Programming   LAST UPDATED: AUGUST 18, 2021

If you want to develop a search service utilizing the power of Google search, you can do so using the google module in Python. You can use this to develop a backend service for a desktop application or implement a website search or app search with the python code running on your server.

Perform Google search using Python

If you are a Python beginner, then you can learn Python from Studytonight.

You can do the same using the BeautifulSoup module too which is used for web scraping in Python. But the google module makes it super simple to implement search.

Installing Google module

To install the google module, we can use the pip package installer.

pip install google

This will install the google module, along with its other dependencies. The name of the module installed is googlesearch.

We will be using the search() function from the googlesearch module.

search(query, tld='co.in', lang='en', num=10, start=0, stop=None, pause=2)
  • query: This is the text that you want to search for.

  • tld: This refers to the top level domain value like co.in or com which will specify which Google website we want to use.

  • lang: This parameter stands for language.

  • num: This is used to specify the number of results we want.

  • start: This is to specify from where to start the results. We should keep it 0 to begin from the very start.

  • stop: The last result to retrieve. Use None to keep searching forever.

  • pause: This parameter is used to specify the number of seconds to pause between consecutive HTTP requests because if we hit too many requests, Google can block our IP address.

The above function will return a python generator (iterator) which has the search result URLs.

Perform Google Search

Now let's use the google module to perform search.

from googlesearch import search

query = "studytonight"

for i in search(query, tld="co.in", num=10, stop=10, pause=2):
    print(i)


https://www.studytonight.com/
https://www.youtube.com/channel/UCURY4RZois483CPY8prhx7g
https://in.linkedin.com/company/studytonight
https://www.crunchbase.com/organization/studytonight
https://edtechreview.in/news/1052-studytonight-easy-lessons-and-free-online-courses-organized-for-students
https://www.instagram.com/study.tonight/?hl=en
https://www.mouthshut.com/websites/Studytonight-com-reviews-925922565
https://craft.co/studytonight
https://www.facebook.com/Studytonight/
https://apkpure.com/studytonight-free-computer-programming-tutorials/com.studytonight.app

In the above output you can see the links that will be shown on Google search if you open the Google search website and search for "studytonight" text.

Similarly, you can search for any text, and can even change the tld parameter to search for results in different Google websites.

Conclusion:

Well, that's it for this tutorial. Try using this python code to get search results from google programmatically. While using this code, do not name the python file as googlesearch.py as that can cause conflict with the module.

You may also like:


IF YOU LIKE IT, THEN SHARE IT

RELATED POSTS