Getting the Google Trends Data with Python

Image for post
Image for post

In this article you will learn how to use pytrends API, save the data as CSV file with pandas, and plot the time series image

Important:The default data type of the API output is “pandas.core.frame.DataFrame”, please feel free to use pandas to manipulate these data.

Before the tutorial, we should know that pytrends isn’t an official API for Google Trends. So maybe one day this package will be banned by Google(Just guessing)

Let’s take a look at the example below:

1.Install Package with pip

2.Connect to API

(Optional): If you are blocked by Google

we can use proxies to bypass it.

3.Print out the result and save it as CSV file

Then you can find that the Py_VS_R.csv is right in your folder, just open it with MS EXCEL or any other software. You will find that there’s a column named isPartial. I don’t know why we have this one column.

But it’s OK, we can drop it with pandas:

4.Save the plot image

Finally our code should look like this:

We open the CSV file again you can find that isPartial column gone away and you will get a image “figure.png”

For more usage about pytrends, please refer to the following link:

Written by

Machine Learning / Deep Learning / Python / Flutter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store