Unofficial API for Google Trends
Allows simple interface for automating downloading of reports from Google Trends. Only good until Google changes their backend again :-P. When that happens feel free to contribute!
Looking for maintainers! Please open an issue with a method of contacting you if you're interested.
pip install pytrends
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360)
or if you want to use proxies as you are blocked due to Google rate limit:
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360, timeout=(10,25), proxies=['https://34.203.233.13:80',], retries=2, backoff_factor=0.1, requests_args={'verify':False})
timeout(connect, read)
tz
'360'
(note NOT -360, Google uses timezone this way...)proxies
['https://34.203.233.13:80','https://35.201.123.31:880', ..., ...]
retries
backoff_factor
{backoff factor} * (2 ^ ({number of total retries} - 1))
seconds. If the backoff_factor is 0.1, then sleep() will sleep for [0.0s, 0.2s, 0.4s, …] between retries. It will never be longer than Retry.BACKOFF_MAX. By default, backoff is disabled (set to 0).requests_args
Note: the parameter hl
specifies host language for accessing Google Trends.
Note: only https proxies will work, and you need to add the port number after the proxy ip address
kw_list = ["Blockchain"]
pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='', gprop='')
Parameters
kw_list
The following API methods are available:
Interest Over Time: returns historical, indexed data for when the keyword was searched most as shown on Google Trends' Interest Over Time section.
Multirange Interest Over Time: returns historical, indexed data similar to interest over time, but across multiple time date ranges.
Historical Hourly Interest: returns historical, indexed, hourly data for when the keyword was searched most as shown on Google Trends' Interest Over Time section. It sends multiple requests to Google, each retrieving one week of hourly data. It seems like this would be the only way to get historical, hourly data.
Interest by Region: returns data for where the keyword is most searched as shown on Google Trends' Interest by Region section.
Related Topics: returns data for the related keywords to a provided keyword shown on Google Trends' Related Topics section.
Related Queries: returns data for the related keywords to a provided keyword shown on Google Trends' Related Queries section.
Trending Searches: returns data for latest trending searches shown on Google Trends' Trending Searches section.
Top Charts: returns the data for a given topic shown in Google Trends' Top Charts section.
Suggestions: returns a list of additional suggested keywords that can be used to refine a trend search.
Many API methods use the following:
kw_list
keywords to get data for
Example ['Pizza']
Up to five terms in a list: ['Pizza', 'Italian', 'Spaghetti', 'Breadsticks', 'Sausage']
Advanced Keywords
"iron"
will have a drop down of "Iron Chemical Element, Iron Cross, Iron Man, etc"
.https://www.google.com/trends/explore#q=%2Fm%2F025rw19&cmpt=q
"/m/025rw19"
is the topic "Iron Chemical Element" to use this with pytrendspytrends.suggestions()
to automate this.cat
cat=
and ends before the next &
or view this wiki page containing all available categories"https://www.google.com/trends/explore#q=pizza&cat=71"
'71'
is the categorygeo
'US'
'US-AL'
'GB-ENG'
tz
'360'
timeframe
Date to start from
Defaults to last 5yrs, 'today 5-y'
.
Everything 'all'
Specific dates, 'YYYY-MM-DD YYYY-MM-DD' example '2016-12-14 2017-01-25'
Specific datetimes, 'YYYY-MM-DDTHH YYYY-MM-DDTHH' example '2017-02-06T10 2017-02-12T07'
Current Time Minus Time Pattern:
By Month: 'today #-m'
where # is the number of months from that date to pull data for
'today 3-m'
would get data from today to 3months agoDaily: 'now #-d'
where # is the number of days from that date to pull data for
'now 7-d'
would get data from the last weekHourly: 'now #-H'
where # is the number of hours from that date to pull data for
'now 1-H'
would get data from the last hourgprop
'images'
images
, news
, youtube
or froogle
(for Google Shopping results)pytrends.interest_over_time()
Returns pandas.Dataframe
pytrends.build_payload(kw_list=['pizza', 'bagel'], timeframe=['2022-09-04 2022-09-10', '2022-09-18 2022-09-24']))
pytrends.multirange_interest_over_time()
Returns pandas.Dataframe. It includes the average in the first row.
pytrends.get_historical_interest(kw_list, year_start=2018, month_start=1, day_start=1, hour_start=0, year_end=2018, month_end=2, day_end=1, hour_end=0, cat=0, geo='', gprop='', sleep=0)
Parameters
kw_list
year_start, month_start, day_start, hour_start, year_end, month_end, day_end, hour_end
sleep
Returns pandas.Dataframe
pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False)
Parameters
resolution
inc_low_vol
inc_geo_code
Returns pandas.DataFrame
pytrends.related_topics()
Returns dictionary of pandas.DataFrames
pytrends.related_queries()
Returns dictionary of pandas.DataFrames
pytrends.trending_searches(pn='united_states') # trending searches in real time for United States
pytrends.trending_searches(pn='japan') # Japan
Returns pandas.DataFrame
pytrends.realtime_trending_searches(pn='US') # realtime search trends for United States
pytrends.realtime_trending_searches(pn='IN') # India
Returns pandas.DataFrame
pytrends.top_charts(date, hl='en-US', tz=300, geo='GLOBAL')
Parameters
date
2019
for the year 2019 Top Chart dataReturns pandas.DataFrame
pytrends.suggestions(keyword)
Parameters
keyword
Returns dictionary
pytrends.categories()
Returns dictionary
See the CONTRIBUTING file.
Major JSON revision ideas taken from pat310's JavaScript library
Connecting to google code heavily based off Stack Overflow post
With some ideas pulled from Matt Reid's Google Trends API