Google Search Analysis with Python
Last Updated :
06 May, 2025
Google handles over billions of searches every day and trillions of searches each year. This shows how important it is to understand what people are searching for and in this article, we’ll learn how to use Python to analyze Google search data focusing on search queries.
Understanding Pytrends
Pytrends is an unofficial Python tool that lets you access Google Trends data. It helps you find out the most popular search topics or subjects on Google. With Pytrends you can explore trends, compare search interest from different places and understand what people are searching for in a better way.
Installing Pytrends
To use this API you first need to install it on your systems. You can easily install it using the following command:
pip install pytrends
Python Implementation of Google Search Analysis
1. Import Necessary Libraries and Connect to Google
We will be using pandas, pytrends, matplotlib and time library for this.
Python
import pandas as pd
from pytrends.request import TrendReq
import matplotlib.pyplot as plt
import time
Trending_topics = TrendReq(hl='en-US', tz=360)
2. Build Payload
Now, we will be creating a dataframe of top 10 countries that search for the term "Cloud Computing". For this we will be using the method build_payload which allows storing a list of keywords that you want to search. In this you can also specify the timeframe and the category to query the data from.
Python
kw_list=["Cloud Computing"]
Trending_topics.build_payload(kw_list,cat=0, timeframe='today 12-m')
time.sleep(5)
3. Interest Over Time
The interest_over_time() method returns the historical indexed data for when the specified keyword was most searched according to the timeframe mentioned in the build payload method.
Python
data = Trending_topics.interest_over_time()
data = data.sort_values(by="Cloud Computing", ascending = False)
data = data.head(10)
print(data)
Output:
Interest in the Topic Over Time4. Historical Hour Interest
The get_historical_interest() allows us to specify periods such as year_start, month_start, day_start, hour_start, year_end, month_end, day_end and hour_end.
Python
kw_list = ["Cloud Computing"]
Trending_topics.build_payload(kw_list, cat=0, timeframe='2024-01-01 2024-02-01', geo='', gprop='')
data = Trending_topics.interest_over_time()
data = data.sort_values(by="Cloud Computing", ascending = False)
data = data.head(10)
print(data)
Output:
Interest in the Topic over a Time Period
5. Interest By Region
Next is the interest_by_region method which lets you know the performance of the keyword per region. It will show results on a scale of 0-100 where 100 indicates the country with the most search and 0 indicates with least search or not enough data.
Python
data = Trending_topics.interest_by_region()
data = data.sort_values(by="Cloud Computing",
ascending = False)
data = data.head(10)
print(data)
Output:
Interest in the Topic by Region6. Visualizing Interest By Region
Python
data.reset_index().plot(x='geoName', y='Cloud Computing',
figsize=(10,5), kind="bar")
plt.style.use('fivethirtyeight')
plt.show()
Output:
Plot for Interest by RegionWhenever a user searches for something about a particular topic on Google there is a high probability that the user will search for more queries related to the same topic. These are known as related queries. Let us find a list of related queries for "Cloud Computing".
Python
try:
Trending_topics.build_payload(kw_list=['Cloud Computing'])
related_queries = Trending_topics.related_queries()
related_queries.values()
except (KeyError, IndexError):
print("No related queries found for 'Cloud Computing'")
Below is the output when we searched for queries related to Cloud Computing.
Output:
No related queries found for 'Cloud Computing'
8. Keyword Suggestions
The suggestions() method helps you to explore what the world is searching for. It returns a list of additional suggested keywords that can be used to filter a trending search on Google.
Python
keywords = Trending_topics.suggestions(
keyword='Cloud Computing')
df = pd.DataFrame(keywords)
df.drop(columns= 'mid')
Output:
Keyword SuggestionsWith this we can find trends in google search history and can be used for various purposes.
You can download the source-code from here.
Similar Reads
Search Google Using Python Selenium
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This art
1 min read
How to Automate Google Sheets with Python?
In this article, we will discuss how to Automate Google Sheets with Python. Pygsheets is a simple python library that can be used to automate Google Sheets through the Google Sheets API. An example use of this library would be to automate the plotting of graphs based on some data in CSV files that w
4 min read
Web crawling with Python
Web crawling is widely used technique to collect data from other websites. It works by visiting web pages, following links and gathering useful information like text, images, or tables. Python has various libraries and frameworks that support web crawling. In this article we will see about web crawl
4 min read
Performing Google Search using Python code
Let's say you are working on a project that needs to do web scraping but you don't know websites on which scraping is to be performed beforehand instead you are required to perform a google search and then proceed according to google search results to a few websites. In that case, you need google se
2 min read
What Can I Do With Python?
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Let's see what Python programming does: Uses of PythonIn terms of
5 min read
Twitter Sentiment Analysis using Python
This article covers the sentiment analysis of any topic by parsing the tweets fetched from Twitter using Python. What is sentiment analysis? Sentiment Analysis is the process of 'computationally' determining whether a piece of writing is positive, negative or neutral. Itâs also known as opinion mini
10 min read
How to Learn Python Basics With ChatGPT
Python is one of the most popular programming languages, known for its simplicity and versatility. Whether you're a complete beginner or an experienced programmer looking to expand your skillset, mastering the basics of Python is essential. In this guide, we'll walk you through the fundamentals of P
4 min read
Using Google Sheets as Database in Python
In this article we'll be discussing how we can use Google Sheets to run like a database for any Python file. Google Spreadsheets:Google Spreadsheets is free online web based application that resembles Microsoft Excel. You can use it to create and edit tables for various projects such as Contact Lis
4 min read
Why is Python So Popular?
One question always comes into people's minds Why Python is so popular? As we know Python, the high-level, versatile programming language, has witnessed an unprecedented surge in popularity over the years. From web development to data science and artificial intelligence, Python has become the go-to
7 min read
What is Python? Its Uses and Applications
Python is a programming language that is interpreted, object-oriented, and considered to be high-level. What is Python? Python is one of the easiest yet most useful programming languages and is widely used in the software industry. People use Python for Competitive Programming, Web Development, and
8 min read