Google Search Analysis with Python

Approximately 3.5 billion searches are performed on Google daily, which means that approximately 40,000 searches are performed every second on Google. So Google search is a great use case for analyzing data based on search queries. With that in mind, in this article, I will walk you through the task of Google search analysis with Python.

Google Search Analysis with Python

Google doesn’t give much access to the data about daily search queries, but another application of google known as Google Trends can be used for the task of Google search analysis. Google Trends provides an API that can be used to analyze the daily searches on Google. This API is known as pytrends, you can easily install it in your systems by using the pip command; pip install pytrends.

I hope you now have easily installed the pytrends library in your systems, now let’s get started with the task of Google search analysis by importing the necessary Python libraries:

import pandas as pd
from pytrends.request import TrendReq
import matplotlib.pyplot as plt
trends = TrendReq()

Here I will be analyzing the Google search trends on the queries based on “Machine Learning”, so let’s create a DataFrame of the top 10 countries which search for “Machine Learning” on Google:

trends.build_payload(kw_list=["Machine Learning"])
data = trends.interest_by_region()
data = data.sort_values(by="Machine Learning", ascending=False)
data = data.head(10)
print(data)
             Machine Learning
geoName                      
Singapore                 100
India                      73
St. Helena                 73
Tunisia                    53
Hong Kong                  51
Pakistan                   51
Sri Lanka                  50
Nepal                      47
South Korea                44
Kenya                      40

So, according to the above results, the search queries based on “Machine learning” are mostly done in Singapore. We can also visualize this data using a bar chart:

data.reset_index().plot(x="geoName", 
                        y="Machine Learning", 
                        figsize=(15,12), kind="bar")
plt.style.use('fivethirtyeight')
plt.show()
Google search analysis using Python

So as we all know that Machine Learning has been the focus of so many companies and students for the last 3-4 years, so let’s have a look at the trend of searches to see how the total search queries based on “Machine Learning” increased or decreased on Google:

data = TrendReq(hl='en-US', tz=360)
data.build_payload(kw_list=['Machine Learning'])
data = data.interest_over_time()
fig, ax = plt.subplots(figsize=(15, 12))
data['Machine Learning'].plot()
plt.style.use('fivethirtyeight')
plt.title('Total Google Searches for Machine Learning', 
          fontweight='bold')
plt.xlabel('Year')
plt.ylabel('Total Count')
plt.show()
trends on google about machine learning

Conclusion

So we can see a huge increase in the searches about “machine learning” on Google in 2022. This is how we can analyze Google searches based on any keyword. A business can perform Google search analysis to understand what people are looking for on Google at any given time. I hope you liked this article on Google search analysis with Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1433

4 Comments

Leave a Reply