Performing Google Search using Python code?



In this article, we will try to do google search using python code, this comes handy in case you are working on a python project and you need to access some data from the web and the search result(from the web) is going to be used inside your project.

Prerequisite –

  • You must have python installed on your system.
  • Install google module. You can use pip to install google module like below −
C:\Users\rajesh>python -m pip install google
Collecting google
Downloading https://files.pythonhosted.org/packages/c8/b1/887e715b39ea7d413a06565713c5ea0e3132156bd6fc2d8b165cee3e559c/google-2.0.1.tar.gz
Requirement already satisfied: beautifulsoup4 in c:\python\python361\lib\site-packages (from google) (4.6.0)
Installing collected packages: google
Running setup.py install for google ... done
Successfully installed google-2.0.1

If all the above prerequisites are done, you can write a code to do google search using python.

Below is the program where the user wants to search specific keyword (for example: “AI in python” or “Tutorialspoint”) and wants all the link (assume top 10 results from the google search) to be used in his python project.

# Performing google search using Python code
class Gsearch_python:
   def __init__(self,name_search):
      self.name = name_search
   def Gsearch(self):
      count = 0
      try :
         from googlesearch import search
      except ImportError:
         print("No Module named 'google' Found")
      for i in search(query=self.name,tld='co.in',lang='en',num=10,stop=1,pause=2):
         count += 1
         print (count)
         print(i + '\n')
if __name__=='__main__':
   gs = Gsearch_python("Tutorialspoint Python")
   gs.Gsearch()

Output

1
https://www.tutorialspoint.com/python/
2
https://www.tutorialspoint.com/python3/
3
https://www.tutorialspoint.com/python_online_training/index.asp
4
https://www.tutorialspoint.com/python/python_overview.htm
5
https://www.tutorialspoint.com/python/python_loops.htm
6
https://www.tutorialspoint.com/python/python_pdf_version.htm
7
https://www.tutorialspoint.com/python/python_basic_syntax.htm
8
https://www.tutorialspoint.com/tutorialslibrary.htm
9
https://www.tutorialspoint.com/
10
https://www.tutorialspoint.com/django/
11
https://www.tutorialspoint.com/numpy
12
https://www.quora.com/I-have-learned-Python-from-Tutorials-Point-What-should-I-do-to-learn-more-topics-so-that-I-can-have-more-advantages-on-my-interviews
13
https://www.pdfdrive.com/python-tutorial-tutorials-point-e10195863.html

The similar result we’ll get when we try to search it through the browser −

Incase we want the query search result directly through a browser instead of giving links of the result, below is the program −

from googlesearch import *
import webbrowser
#to search, will ask search query at the time of execution
query = input("Input your query:")
#iexplorer_path = r'C:\Program Files (x86)\Internet Explorer\iexplore.exe %s'
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
webbrowser.open("https://google.com/search?q=%s" % query)

Output

>>>
=============== RESTART: C:/Python/Python361/google_search1.py ===============
Input your query:Tutorialspoint

Above I searched for a query “tutorialspoint”, and it will pop-up a browser window with −

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements