Coding STEM

Searching online (Google) with Python

In this blog, I demonstrate how to run a Google query from a Python app extremely easily.

The harder part is just the set up and knowing what to install. So, let’s start with the set up details:

You’ll need to install the following packages IN ORDER: 1) beautifulsoup4 2) google
NOTE: The package to install is named “google” but in code, we need to import “search” from “googlesearch” instead of “google”. This trips up most people as the documentation isn’t very clear and I learned after some trial and error.

The right way to import it is with an exception handling as below:

Then call the search() function passing the correct parameters. Again, documentation wasn’t very thorough but this is what I could make out about the function:

urls=search(query, tld, num, stop, pause)


query : query string that we want to search for.
tld : top level domain that we want to search (e.g. .com, .net, etc.)…except you don’t need to supply the dot character.

lang : lang stands for language. Defaults to English if left out.

num : Number of results we want.

stop : How many results to show maximum. 10 for top 10. Leave it out or None to keep searching forever.

pause : Lapse to wait between HTTP requests. Lapse too short may cause Google to block your IP.

The function returns the URLs of the site or page found (one at a time).

Then let’s get an input from the user as a string:

Now that we have the string to search for, let’s pass it to search() function and wrap it in a loop so that we can get and show each URL found after each iteration:

In this example, I’m searching .COM tld and showing top 5 results of the search word supplied by the user. Each result is stored in j variable and for loop iterates until search() ends…which is up to 5 times here.

That’s it! Here are two sample outputs (my code is saved as

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top