Google.com Get Number Of Search Results For Query

On this page you'll learn to grab the number of results & time taken for a query on Google.com

For this example we'll be creating a simple Google auto-searcher that grabs the total number of results for a query & the time take to execute the query.

First, we'll create a function that takes in a search query and prints the number of results found + time taken to execute the query:

from simpleseleniumwrapper import WebDriver as SimpleSelenium #Imports Simple Selenium

def getNumberOfSearchResultsForQuery(query):
    #... Code for this example will go here

Now inside the function let's initiate the Simple Selenium Webdriver:

Visit google.com:

Looking at the search bar for google.com, I notice that the tag is a "textarea", which is a fairly uncommon element on most webpages. Let's try using the textarea to locate the search bar element:

Now we need Simple Selenium to type in the search query. Luckily Simple Selenium has the .write function, which will automatically type text like a human:

After typing, we need to hit the enter key:

Once Simple Selenium hits the enter key, it's time to grab the number of results returned text from the results page.

Looking at the page, the results returned text has an ID "result-stats" that we can use to grab the text element:

Let's use that ID to locate the number of results found text on the page:

The last step is dissecting the text & grabbing valuable information. We can use Python's .split function to do this:

Now let's print the number of results found & time taken for the query:

And finally, close the Webdriver cleanly:

The entire function code looks like this:

Let's test the function out:

Simple Selenium types the query like a human
Results page has the number of results & time taken to execute query
The final printout of this function

As you can see from the pictures above, the function works! Hopefully this example helps inspire you to build your own projects using Simple Selenium.

Last updated