Programmatic Google Trends API
By Sal Uryasev
June 11, 2008
Find more about:
google
trends
api
programmatic
Yesterday, Google released an update to their popular Google Trends tool. There are improvements over the previous version, but the biggest new feature is a new shiny button that lets you download all your data in the format of a CSV. This is a very cool enhancement. Where Google Trends was a geeky toy, it now takes the leap to integrate into analysts' reports and with that, edge its way onto managerial desks.
This python module is a quasi-API to make it easier to authenticate into Google Trends for those who want to squeeze the extra level of functionality out of their data. The advantage of programmatic access is that the data can be automatically trended and merged. It can be snuck into a 9:00 AM daily email to the VP of Marketing so that she knows to ramp up Google Adwords campaigns for some specific keyword. Also, by programatically pulling multiple reports, it is possible to create a wealth of data not visible in a single report. Using one keyword as a benchmark to merge multiple reports, we can do a meaningful comparison on tens or hundreds of relevant keywords.
To use the pyGTrends, the quasi-Google-Trends-API, download the file from our server.
Here is an example of the most basic basic report that you can pull down from Google Trends. The connector function needs authentication info, and download_report needs to be passed a list of keywords.
from pyGTrends import pyGTrends
connector = pyGTrends('google username','google password')
connector.download_report(('keyword1', 'keyword2'))
print connector.csv()
You can, however, use pyGTrends to get any slice of data that you can pull down from Google Trends. To see the exact parameters that you should use, go to Google Trends, and navigate to the specific sufficiently-narrow report that you are interested in. Then, right-click on the CSV download, and save the link location. The different parameters should be discernible from the link. The following code downloads a report for banana, bread, and bakery keywords from April 2008, originating from the magnificent nation of Austria, and scaled using fixed scaling (aka the second download link).
connector.download_report(('banana', 'bread', 'bakery'),
date='2008-4',
geo='AT',
scale=1)
By default, the csv() function downloads the main part of the report, but there are a few additional parts stuck to the bottom of the CSV file. If you are interested in those, pass the section parameter to the csv() function. If you do not want column headers on your data, you can also pass the column_headers parameter as false. The following will return the Language section without any headers.
print connector.csv(section='Language', column_headers=False)
Here is a snapshot from the new Google Trends to add some eye-candy to the post:



8 comments | Show all comments only the last 5 are shown
yadab das said:
The PyGTrends.py API looks really fascinating to me. I have almost converted the Code to Java and will publish this week with a Swing Interface. Any better suggestions on that?
Archie said:
Hi Yadab! Could you please share the Java code you have written? I am also working on it. Please contact me by email.
aavaliani (at) gmx.net
Gautham Ramachandran said:
The PyGTrends.py API is really awesome. I have a question though. Does Google frown upon iterative pings to Google Trends to pull Relative traffic. To make it more specific, if I have 2,000 keywords and I code iterative pulls from Google trends, do I stand a chance of getting banned?
Gautham.
Sal Uryasev said:
Yes and No.
I believe that Google tends to be generous regarding use of their services. They want people to get the maximum utility out of the products, but they don't want their generosity to be abused. The login requirement for downloading the Google Trends data is probably there just for that reason. The cap is probably quite large, but there certainly is one. I wouldn't build a webservice (without having users use their own account). You may have more luck if you lump many keywords per call, and spread out your data gathering over longer periods of time.
James Solo said:
This Python script is great and provides an excellent solution. However, I have never used Python before so I was hoping someone could email (james.solo |AT| mathworks.com) me step by step instructions on how to modify this script to work with keywords of my choice ( I have 40 total ) and to grab data from 2004 to-date using the "CSV with relative scaling" data file.
Many thanks,
James
Arjun said:
I have been using the pyGTrends module and have encountered problems when using keywords with more than one word. For instance, "air express" was one of the keywords. It has a search history--when I manually download the data from Google Trends, the historical data shows up fine. However, when I use the pyGTrends module, the data comes out as all zeroes.
The same problem occurs with all keywords/phrases that contain more than one word. Is pyGTrends compatible with only one-word keywords, and if not, how do I fix this problem?
If someone could email me, arjunrmodi at gmail dot com, that would be great. Thanks.
A said:
Great module.
Sal Uryasev said:
Arjun: It should work now. Thanks for pointing it out!
said:
Add a comment