-
-
Save nivertech/5652295 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Downloads a CSV file from Google Trends with the query 'debt'. | |
| Reads in this CSV file, cleans it up, and creates file called 'trends-debt.csv' in the current working directory. | |
| Requires your Google login credentials (meaning you need to edit this script). | |
| Requires <https://github.com/pedrofaustino/google-trends-csv-downloader>. | |
| """ | |
| import csv | |
| import re | |
| from pyGoogleTrendsCsvDownloader import pyGoogleTrendsCsvDownloader | |
| # username and password are your Google login credentials | |
| r = pyGoogleTrendsCsvDownloader(username, password) | |
| r.get_csv(q='debt') # Saves a file 'trends_q-debt.csv' in the current working directory | |
| with open('trends_q-debt.csv', 'r') as f: | |
| reader = csv.reader(f) | |
| dates = [] | |
| values = [] | |
| for row in reader: | |
| try: | |
| date, value = row | |
| except ValueError: | |
| continue | |
| if re.search('[0-9]{4}-[0-9]{2}-[0-9]{2}', date): | |
| dates.append(date[-10:]) # Uses last date in time period | |
| values.append(value) | |
| with open('trends-debt.csv', 'w') as f: | |
| writer = csv.writer(f) | |
| writer.writerow(['date', 'debt']) | |
| for row in zip(dates, values): | |
| writer.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment