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
Proxy scraper. Format: IP | PORT | COUNTRY | TYPE

proxy scraper 🔎 Installation: git clone https://github.com/ebankoff/proxy_scraper Required pip libraries (pip install library name): lxml beautifulso

Eban'ko 19 Dec 07, 2022
Web scraping library and command-line tool for text discovery and extraction (main content, metadata, comments)

trafilatura: Web scraping tool for text discovery and retrieval Description Trafilatura is a Python package and command-line tool which seamlessly dow

Adrien Barbaresi 704 Jan 06, 2023
Web scraper build using python.

Web Scraper This project is made in pyhthon. It took some info. from website list then add them into data.json file. The dependencies used are: reques

Shashwat Harsh 2 Jul 22, 2022
京东抢茅台,秒杀成功很多次讨论,天猫抢购,赚钱交流等。

Jd_Seckill 特别声明: 请添加个人微信:19972009719 进群交流讨论 目前群里很多人抢到【扫描微信添加群就好,满200关闭群,有喜欢薅信用卡羊毛的也可以找我交流】 本仓库发布的jd_seckill项目中涉及的任何脚本,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性

50 Jan 05, 2023
Web Scraping Framework

Grab Framework Documentation Installation $ pip install -U grab See details about installing Grab on different platforms here http://docs.grablib.

2.3k Jan 04, 2023
A multithreaded tool for searching and downloading images from popular search engines. It is straightforward to set up and run!

🕳️ CygnusX1 Code by Trong-Dat Ngo. Overviews 🕳️ CygnusX1 is a multithreaded tool 🛠️ , used to search and download images from popular search engine

DatNgo 32 Dec 31, 2022
Binance Smart Chain Contract Scraper + Contract Evaluator

Pulls Binance Smart Chain feed of newly-verified contracts every 30 seconds, then checks their contract code for links to socials.Returns only those with socials information included, and then submit

14 Dec 09, 2022
AssistScraper - program for /r/nba to use to find list of all players a player assisted and how many assists each player recieved

AssistScraper - program for /r/nba to use to find list of all players a player assisted and how many assists each player recieved

5 Nov 25, 2021
An application that on a given url, crowls a web page and gets all words, sorts and counts them.

Web-Scrapping-1 An application that on a given url, crowls a web page and gets all words, sorts and counts them. Installation Using the package manage

adriano atambo 1 Jan 16, 2022
👁️ Tool for Data Extraction and Web Requests.

httpmapper 👁️ Project • Technologies • Installation • How it works • License Project 🚧 For educational purposes. This is a project that I developed,

15 Dec 05, 2021
A scrapy pipeline that provides an easy way to store files and images using various folder structures.

scrapy-folder-tree This is a scrapy pipeline that provides an easy way to store files and images using various folder structures. Supported folder str

Panagiotis Simakis 7 Oct 23, 2022
A way to scrape sports streams for use with Jellyfin.

Sportyfin Description Stream sports events straight from your Jellyfin server. Sportyfin allows users to scrape for live streamed events and watch str

axelmierczuk 38 Nov 05, 2022
京东茅台抢购最新优化版本,京东秒杀,添加误差时间调整,优化了茅台抢购进程队列

京东茅台抢购最新优化版本,京东秒杀,添加误差时间调整,优化了茅台抢购进程队列

776 Jul 28, 2021
An IpVanish Proxies Scraper

EzProxies Tired of searching for good proxies for hours? Just get an IpVanish account and get thousands of good proxies in few seconds! Showcase Watch

11 Nov 13, 2022
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
Scrapping Connections' info on Linkedin

Scrapping Connections' info on Linkedin

MohammadReza Ardestani 1 Feb 11, 2022
This Scrapy project uses Redis and Kafka to create a distributed on demand scraping cluster

This Scrapy project uses Redis and Kafka to create a distributed on demand scraping cluster.

IST Research 1.1k Jan 06, 2023
Generate a repository with mirror links for DriveDroid app

DriveDroid Repository Generator Generate a repository for the app that allow boot a PC using ISO files stored on your Android phone Check also an offi

Evgeny 11 Nov 19, 2022
Web scrapping

Project Setup Table of Contents Project Setup Table of Contents Run project locally Install Requirements Run script Run project locally Install Requir

Charles 3 Feb 04, 2022
A simplistic scraper made to download tons of random screenshots made by people.

printStealer 1.1 What is this tool? This tool is developed to show the insecurity of the screenshot utility called prnt sc. It is a site that stores s

appelsiensam 4 Jul 26, 2022