Let's perform Google search with python

Written by raunasur on Apr 30th, 2021 Views Report Post

This is my first post and in this post you will learn how to perform google search with python. Firstly you will need install beautifulsoup as google has a dependency on it. So,

pip install beautifulsoup4

After this install google,

pip install google

Now, you are ready. The syntax is

search(query, tld=’com’, lang=’en’, num=10, start=0, 
stop=None, pause=2.0)

Where, query : query string that we want to search for. tld : tld stands for top level domain which means we want to search our result on google.com or google.in or some other domain. lang : lang stands for language. num : Number of results we want. start : First result to retrieve. stop : Last result to retrieve. Use None to keep searching forever. pause : Lapse to wait between HTTP requests. Lapse too short may cause Google to block your IP. Keeping significant lapse will make your program slow but its safe and better option. Return : Generator (iterator) that yields found URLs. If the stop parameter is None the iterator will loop forever. So, this is the final code,

try: 
	from googlesearch import search 
except ImportError: 
	print("No module named ‘google’ found") 

# to search 
query = "python"

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

This will print,

https://www.python.org

Now, it’s your turn to search. Happy Coding!

Comments (0)