Skip to main content

Pull and merge data from the Google Trends for Health API

This code has no license. Theodore L Caputi (Copyright 2020) retains all rights over all code in this package. Unauthorized use, including reuse, modification, and distribution, is forbidden.

Usage

To use the gtrends package for Python3, you'll need to request an API Key from Google. You can do that easily here.

The main function of the package is theo_timeline, a function that pulls and merges data from the Google Trends API and saves a single CSV file for each term (dates on the index and geographies as the columns). Note that multiple files will be created if multiple timeframes are requested. An example script would be:

from gtrendspy import timeline

timeline.theo_timeline(
    terms = ['cat', 'cat + food'],
    names = ['cat', 'food'],
    start = '2018-01-01',
    end = '2020-02-01',
    timeframe_list = ['month'],
    geo_country_list = ['US', 'CA'],
    worldwide = False,
    timestep_years = 1,
    batch_size = 2,
    us_states = False,
    outpath = "/path/to/output/directory",
    creds = "/path/to/info.txt"
)

This script would pull monthly data for the search terms cat and cat + food for both the US and Canada between 1 Jan 2018 and 1 Feb 2020, and then reformat that data into two CSV files (cat_month.csv and food_month.csv, respectively). Each of these CSV files would be formatted such that the first column (timestamp) gives the month and the second and third columns (US and CA) give search queries for the US and Canada, respectively.

This package is intended to be used in conjunction with the gtrendR package for R.

Getting Started

Note: This package assumes use of Python 3.X and pip3. It works on Linux and Windows OS.

Install from pip

(1) pip install from Github

pip install gtrendspy

(2) pip install requirements.txt

pip install -r requirements.txt

(3) Create a creds file setting your own parameters (DISCOVERY_URL, API_KEY)

Your creds file should look something like this. You'll have access to both once Google approves your request for an API Key.

DISCOVERY_URL = "XXX"
API_KEY = "XXX"

(4) Begin using the package! The merged output will be in the directory you named as outpath.

Tarball

(1) Request tarball (gtrends.tar.gz) from tcaputi@gmail.com

(2) Run the command:

pip install --upgrade /path/to/gtrends.tar.gz

(3) Install dependencies using the command:

pip install -r /path/to/gtrends/requirements.txt

(4) Create a creds file setting your own parameters (see above)

(5) Begin using the package! The merged output will be in the directory you named as outpath.

Release files for gtrendspy 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gtrendspy 1.0.6
File Size Uploaded
gtrendspy-1.0.6.tar.gz 7.2 kB Details

Release files / gtrendspy-1.0.6.tar.gz

Download URL gtrendspy-1.0.6.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
46ff9127482044331ce67a0d08e39cb4104386ab2220129bc1df80e0e7e6531d
BLAKE2b-256 checksum
How to use checksums
796e6fd93b5b2bc83af596e948b6dd9ba46c0a15ca46efcc7b8704752bfd0b73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

1.0.6 This release

1 release file

1.0.5

2 release files

1.0.4

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page