Pseudo API for Google Trends

Related tags

Web Crawlingpytrends
Overview

pytrends

Introduction

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!

Table of contens

Installation

pip install pytrends

Requirements

  • Written for Python 3.3+
  • Requires Requests, lxml, Pandas

back to top

API

Connect to Google

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

    • Timezone Offset
    • For example US CST is '360' (note NOT -360, Google uses timezone this way...)
  • proxies

    • https proxies Google passed ONLY
    • list ['https://34.203.233.13:80','https://35.201.123.31:880', ..., ...]
  • retries

    • number of retries total/connect/read all represented by one scalar
  • backoff_factor

    • A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a second try without a delay). urllib3 will sleep for: {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

    • A dict with additional parameters to pass along to the underlying requests library, for example verify=False to ignore SSL errors

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

Build Payload

kw_list = ["Blockchain"]
pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='', gprop='')

Parameters

  • kw_list

    • Required
    • Keywords to get data for

back to top

API Methods

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.

  • 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.

back to top

Common API parameters

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

        • When using Google Trends dashboard Google may provide suggested narrowed search terms.
        • For example "iron" will have a drop down of "Iron Chemical Element, Iron Cross, Iron Man, etc".
        • Find the encoded topic by using the get_suggestions() function and choose the most relevant one for you.
        • For example: https://www.google.com/trends/explore#q=%2Fm%2F025rw19&cmpt=q
        • "%2Fm%2F025rw19" is the topic "Iron Chemical Element" to use this with pytrends
        • You can also use pytrends.suggestions() to automate this.
  • cat

    • Category to narrow results
    • Find available cateogies by inspecting the url when manually using Google Trends. The category starts after cat= and ends before the next & or view this wiki page containing all available categories
    • For example: "https://www.google.com/trends/explore#q=pizza&cat=71"
    • '71' is the category
    • Defaults to no category
  • geo

    • Two letter country abbreviation
    • For example United States is 'US'
    • Defaults to World
    • More detail available for States/Provinces by specifying additonal abbreviations
    • For example: Alabama would be 'US-AL'
    • For example: England would be 'GB-ENG'
  • tz

  • 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'

      • Note Time component is based off UTC
    • Current Time Minus Time Pattern:

      • By Month: 'today #-m' where # is the number of months from that date to pull data for

        • For example: 'today 3-m' would get data from today to 3months ago
        • NOTE Google uses UTC date as 'today'
        • Works for 1, 3, 12 months only!
      • Daily: 'now #-d' where # is the number of days from that date to pull data for

        • For example: 'now 7-d' would get data from the last week
        • Works for 1, 7 days only!
      • Hourly: 'now #-H' where # is the number of hours from that date to pull data for

        • For example: 'now 1-H' would get data from the last hour
        • Works for 1, 4 hours only!
  • gprop

    • What Google property to filter to
    • Example 'images'
    • Defaults to web searches
    • Can be images, news, youtube or froogle (for Google Shopping results)

back to top

Interest Over Time

pytrends.interest_over_time()

Returns pandas.Dataframe

back to top

Historical Hourly Interest

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

    • Required
    • list of keywords that you would like the historical data
  • year_start, month_start, day_start, hour_start, year_end, month_end, day_end, hour_end

    • the time period for which you would like the historical data
  • sleep

    • If you are rate-limited by Google, you should set this parameter to something (i.e. 60) to space off each API call.

Returns pandas.Dataframe

back to top

Interest by Region

pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False)

Parameters

  • resolution

    • 'CITY' returns city level data
    • 'COUNTRY' returns country level data
    • 'DMA' returns Metro level data
    • 'REGION' returns Region level data
  • inc_low_vol

    • True/False (includes google trends data for low volume countries/regions as well)
  • inc_geo_code

    • True/False (includes ISO codes of countries along with the names in the data)

Returns pandas.DataFrame

back to top

Related Topics

pytrends.related_topics()

Returns dictionary of pandas.DataFrames

back to top

Related Queries

pytrends.related_queries()

Returns dictionary of pandas.DataFrames

back to top

Trending Searches

pytrends.trending_searches(pn='united_states') # trending searches in real time for United States
pytrends.trending_searches(pn='japan') # Japan

Returns pandas.DataFrame

back to top

Top Charts

pytrends.top_charts(date, hl='en-US', tz=300, geo='GLOBAL')

Parameters

  • date

    • Required
    • YYYY integer
    • Example 2019 for the year 2019 Top Chart data
    • Note Google removed support for monthly queries (e.g. YYYY-MM)
    • Note Google does not return data for the current year

Returns pandas.DataFrame

back to top

Suggestions

pytrends.suggestions(keyword)

Parameters

  • keyword

    • Required
    • keyword to get suggestions for

Returns dictionary

back to top

Categories

pytrends.categories()

Returns dictionary

back to top

Caveats

  • This is not an official or supported API
  • Google may change aggregation level for items with very large or very small search volume
  • Rate Limit is not publicly known, let me know if you have a consistent estimate
    • One user reports that 1,400 sequential requests of a 4 hours timeframe got them to the limit. (Replicated on 2 networks)
    • It has been tested, and 60 seconds of sleep between requests (successful or not) is the correct amount once you reach the limit.
  • For certain configurations the dependency lib certifi requires the environment variable REQUESTS_CA_BUNDLE to be explicitly set and exported. This variable must contain the path where the ca-certificates are saved or a SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] error is given at runtime.

Credits

Comments
  • 429 After first request?

    429 After first request?

    Howdy,

    I'm attempting to use the library and I'm getting hit with a 429 error after copying the example code. Here's my script:

    from pytrends.request import TrendReq
    
    pytrends = TrendReq(hl='en-US', tz=360)
    kw_list = ["Blockchain"]
    pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='', gprop='')
    pytrends.interest_over_time()
    

    I can visit the trends website fine, and I can copy and paste the URL produced by the API and get a json file just fine. It's hard for me to imagine being rate limited on my first request and still being able to visit the site normally.

    Any ideas?

    opened by alexsullivan114 41
  • RateLimitError

    RateLimitError

    Hi!

    Thank you for the updates of the code. I tried to run the new updated version. After about 10 downloads, I receive the following traceback:

    Traceback (most recent call last): File "C:/Users/Documents/Python Scripts/collect_gtrends.py", line 34, in trend=pytrend.trend(trend_payload, return_type='dataframe') File "C:\Users\AppData\Roaming\Python\Python27\site-packages\pytrends\request.py", line 62, in trend raise RateLimitError pytrends.request.RateLimitError

    I don't think this is the quota limit problem. Maybe I was downloading too frequently? How may seconds do you guys wait in between requests? My current program lets it sleep for 5-10 seconds. Is that not enough? Thank you!

    opened by sarahjohns 29
  • Is it just me or have 429 errors really increase these past few days?

    Is it just me or have 429 errors really increase these past few days?

    I used to get 429 error every time I request more than 6 items per hour or so.

    But recently, especially today I am not able to request more than 1 per hour without getting 429. Is it just my IP acting up?

    opened by igalci 23
  • ModuleNotFoundError: No module named 'pandas.io.json.normalize'

    ModuleNotFoundError: No module named 'pandas.io.json.normalize'

    I have pandas, lxml, numpy, json modules. But i've got this error when i run example codes of pytrends.

    ModuleNotFoundError: No module named 'pandas.io.json.normalize'

    opened by kubilaykilinc 20
  • Python 2 compatibility issues

    Python 2 compatibility issues

    Hi all,

    I'm having troubles using this library with pyhton2 when there is an error in the response. In fact JSONDecodeError that is being caught when parsing the response is not defined in Python 2 (as stated in https://docs.python.org/3/library/json.html#json.JSONDecodeError). It is also stated that JSONDecodeError is a subclass of ValueError, which could be used for the python2 version.

    Thanks, Luca

    bug 
    opened by covix 20
  • Google Quota limit - IP Address changer

    Google Quota limit - IP Address changer

    Hi, As mentionned before in previous issues I face the Google quota limit after barely 10 requests. I tried to change my IP address by routing the requests through Tor. However I have not been able yet to bypass the limitation. I raise the issue in the following page: http://stackoverflow.com/questions/40406458/google-trends-quota-limit-ip-address-changer

    opened by jblemoine 18
  • interest_over_time doesn't work

    interest_over_time doesn't work

    Hi, I have the following issue:

    Using your example I execute the following code: pytrend.build_payload(kw_list=['pizza', 'bagel']) pytrend.interest_over_time()

    After the last one I have an answer "ValueError: year is out of range"

    And the following: pytrend.interest_by_region() gives me : ValueError: No JSON object could be decoded

    At the same time pytrend.related_queries() works well.

    What could be wrong here?

    opened by FourthWiz 15
  • request for tests

    request for tests

    Merging PRs is difficult for the maintainers because of a lack of robust tests. If somebody writes a broad set of tests, it will significantly improve the ability to merge updates with less risk.

    Thank you for your help!

    help wanted good first issue 
    opened by emlazzarin 14
  • Script stopped working.. 400 Bad Request error

    Script stopped working.. 400 Bad Request error

    I have been using this python script for long now... suddenly today this script stopped working. I am getting 400 Bad Request error and now able to download any Google Trend CSV file from the script..

    Getting error for Connector as well. "connector = pyGTrends(google_username, google_password)"

    I think this is the main issue.

    opened by ravimevcha 14
  • Trends with daily granularity

    Trends with daily granularity

    I think that this is not an issue because Google decides the granularity of the results (daily, weekly or monthly) depending on the search time frame. So I decided to implement a method that splits big time frames into smaller ones (90 days i.e.) with a one day overlap to normalize the scale between the data. Do you think this could be a good improvement for pytrends?

    help wanted 
    opened by bigme666 13
  • SSL cert failure on VPN

    SSL cert failure on VPN

    When I try to login using TrendReq(..) I am getting a SSL error. From what I've been able to figure out is that the form from https://accounts.google.com/ServiceLogin doesn't seem to accept the Passwd arguments until you make the request with the email and then you have to make a new request again with the password. Not sure if this is on the right track or I'm just being an idiot.

    opened by ZenW00kie 13
  • Combination of interest_over_time() and interest_by_region()

    Combination of interest_over_time() and interest_by_region()

    Hey there, I was wondering if it is possible to request data over time with a defined geographical resolution. Currently, it is only possible to have either a temporal or a spatial differentiation, but not both at the same time. Since different Google Trends API URLs are used for the two requests, I think Google Trends may restrict this option. Thanks!

    opened by MoritzDPTV 0
  • Getting incomplete data requesting timeframe=all

    Getting incomplete data requesting timeframe=all

    So Im searching for a specific term and my request gets back with a data gap, while checking the data on the google website and donwloading data comes complete. Also when requesting data for the specific gap I get the correct data points.

    ` kw_list = ['Arbeitslosigkeit'] pytrends.build_payload(kw_list, cat=0, timeframe='2010-01-01 2012-12-25', geo='DE', gprop='')

    pytrends.build_payload(kw_list, cat=0, timeframe='all', geo='DE', gprop='')

    `

    pic1 marcc

    opened by RogerRendon 1
  •  Cannot get the same result as the webpage

    Cannot get the same result as the webpage

    I use interest_over_time() can not get the same result as the webpage, I notice the webpage's headers['req'] is diffrent from the requests, i change it as the webpage's, but still cannot get the same result, what should i do?

    the webpage's headers['req'] is in below, Some do not seem to exist before? Is this the reason? req: {"time":"2004-01-01 2022-11-17","resolution":"MONTH","locale":"zh-CN","comparisonItem":[{"geo":{"country":"BR"},"complexKeywordsRestriction":{"keyword":[{"type":"ENTITY","value":"/m/01hpbc"}]}},{"geo":{"country":"BR"},"complexKeywordsRestriction":{"keyword":[{"type":"ENTITY","value":"/g/11dymw9wxl"}]}}],"requestOptions":{"property":"","backend":"IZG","category":0},"userConfig":{"userType":"USER_TYPE_LEGIT_USER"}}

    opened by jmz1996 2
  •  Interest_over_time missing data

    Interest_over_time missing data

    Today I started facing an issue with the Interest_over_time missing data.

    The trend data just drops to 0 for about a year or so then the data picks back up.

    Last night I had no issues then this morning it started. Tested on multiple machines and different networks.

    For example, try running Interest_over_time for the keyword "barefoot shoes" you'll see around 2020 the data goes to 0 and then returns to normal.

    It only happens for some keywords while others are fine.

    Anyone else facing this issue?

    opened by nicktba 3
  • Newbie: specification of years

    Newbie: specification of years "today 5-y" works but not "today 10-y"

    This could be a newbie issue. Are there restrictions on the years valid in the timescale parameter ? I can get the payload to work with "today 5-y" but not today 10-y". The "all" parameter works "all" - I note that in other parts of the api there are specific limits - are years restricted to 5 or all ? thanks

    opened by loquor 0
  • No way to know what changed between versions

    No way to know what changed between versions

    Currently there is no way to know what changed between versions except to download both versions from pypi and check the differences in the source code, this makes very risky to depend on this library for anything non-amateurish.

    Please consider adding one or more of the following:

    • Release in Github with changelog.
    • Annotated tags in the commit where the version is released.
    • Add a CHANGELOG.md file to the repo with a header for every version released; bonus points if you follow the Keep a changelog format.
    • Add a changelog section in the README.md with a header for every version released.
    opened by Terseus 2
Releases(v4.8.0)
Owner
General Mills
General Mills
This is a module that I had created along with my friend. It's a basic web scraping module

QuickInfo PYPI link : https://pypi.org/project/quickinfo/ This is the library that you've all been searching for, it's built for developers and allows

OneBit 2 Dec 13, 2021
Parse feeds in Python

feedparser - Parse Atom and RSS feeds in Python. Copyright 2010-2020 Kurt McKee Kurt McKee 1.5k Dec 30, 2022

Simple tool to scrape and download cross country ski timings and results from live.skidor.com

LiveSkidorDownload Simple tool to scrape and download cross country ski timings

0 Jan 07, 2022
Web-Scrapper using Python and Flask

Web-Scrapper "[초급]Python으로 웹 스크래퍼 만들기" 코스 -NomadCoders 기초적인 Python 문법강의부터 시작하여 웹사이트의 html파일에서 원하는 내용을 Scrapping해서 출력, csv 파일로 저장, flask를 이용한 간단한 웹페이지

윤성도 1 Nov 10, 2021
Nekopoi scraper using python3

Features Scrap from url Todo [+] Search by genre [+] Search by query [+] Scrap from homepage Example # Hentai Scraper from nekopoi import Hent

MhankBarBar 9 Apr 06, 2022
A python tool to scrape NFT's off of OpenSea

Right Click Bot A script to download NFT PNG's from OpenSea. All the NFT's you could ever want, no blockchain, for free. Usage Must Use Python 3! Auto

15 Jul 16, 2022
Binance harvester - A Python 3 script to harvest data from the Binance socket stream and calculate popular TA indicators and produce lists of top trending coins

Binance harvester - A Python 3 script to harvest data from the Binance socket stream and calculate popular TA indicators and produce lists of top trending coins

68 Oct 08, 2022
A package designed to scrape data from Yahoo Finance.

yahoostock A package designed to scrape data from Yahoo Finance. Installation The most simple installation method is through PIP. pip install yahoosto

Rohan Singh 2 May 28, 2022
优化版本的京东茅台抢购神器

优化版本的京东茅台抢购神器

1.8k Mar 18, 2022
Example of scraping a paginated API endpoint and dumping the data into a DB

Provider API Scraper Example Example of scraping a paginated API endpoint and dumping the data into a DB. Pre-requisits Python = 3.9 Pipenv Setup # i

Alex Skobelev 1 Oct 20, 2021
a high-performance, lightweight and human friendly serving engine for scrapy

a high-performance, lightweight and human friendly serving engine for scrapy

Speakol Ads 30 Mar 01, 2022
Comment Webpage Screenshot is a GitHub Action that captures screenshots of web pages and HTML files located in the repository

Comment Webpage Screenshot is a GitHub Action that helps maintainers visually review HTML file changes introduced on a Pull Request by adding comments with the screenshots of the latest HTML file cha

Maksudul Haque 21 Sep 29, 2022
An helper library to scrape data from Instagram effortlessly, using the Influencer Hunters APIs.

Instagram Scraper An utility library to scrape data from Instagram hassle-free Go to the website » View Demo · Report Bug · Request Feature About The

2 Jul 06, 2022
Universal Reddit Scraper - A comprehensive Reddit scraping command-line tool written in Python.

Universal Reddit Scraper - A comprehensive Reddit scraping command-line tool written in Python.

Joseph Lai 543 Jan 03, 2023
腾讯课堂,模拟登陆,获取课程信息,视频下载,视频解密。

腾讯课堂脚本 要学一些东西,但腾讯课堂不支持自定义变速,播放时有水印,且有些老师的课一遍不够看,于是这个脚本诞生了。 时间比较紧张,只会不定时修复重大bug。多线程下载之类的功能更新短期内不会有,如果你想一起完善这个脚本,欢迎pr 2020.5.22测试可用 使用方法 很简单,三部完成 下载代码,

163 Dec 30, 2022
Minecraft Item Scraper

Minecraft Item Scraper To run, first ensure you have the BeautifulSoup module: pip install bs4 Then run, python minecraft_items.py folder-to-save-ima

Jaedan Calder 1 Dec 29, 2021
Script for scrape user data like "id,username,fullname,followers,tweets .. etc" by Twitter's search engine .

TwitterScraper Script for scrape user data like "id,username,fullname,followers,tweets .. etc" by Twitter's search engine . Screenshot Data Users Only

Remax Alghamdi 19 Nov 17, 2022
Libextract: extract data from websites

Libextract is a statistics-enabled data extraction library that works on HTML and XML documents and written in Python

499 Dec 09, 2022
Scrape and display grades onto the console

WebScrapeGrades About The Project This Project is a personal project where I learned how to webscrape using python requests. Being able to get request

Cyrus Baybay 1 Oct 23, 2021
OSTA web scraper, for checking the status of school buses in Ottawa

OSTA-La-Vista OSTA web scraper, for checking the status of school buses in Ottawa. Getting Started Using a Raspberry Pi, download Python 3, and option

1 Jan 28, 2022