Created
March 20, 2016 08:17
-
-
Save Lvl4Sword/dbc62ec169019aea8315 to your computer and use it in GitHub Desktop.
Google Trends Search
This file contains 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
from requests import get | |
from yaml import load | |
import sys | |
# in a virtualenv: pip install libyaml | |
# input is always split by a comma | |
# if you were searching for 2 things it would be q=a,b& | |
base = 'https://www.google.com/trends/fetchComponent?hl=en-US&q=[INPUT_HERE]&cid=TIMESERIES_GRAPH_0&export=5' | |
# better to be explicit than implicit | |
# this makes sure that all search terms are actually caught | |
split = base.split('https://www.google.com/trends/fetchComponent?hl=en-US&q=')[1].split('&cid=TIMESERIES_GRAPH_0&export=5')[0].split(',') | |
dupes = [] | |
for item in split: | |
if item not in dupes: | |
dupes.append(item) | |
if dupes == ['[INPUT_HERE]'] or dupes[0] == '[INPUT_HERE]': | |
print('[INPUT_HERE] text was not deleted from base variable on line 9.') | |
sys.exit() | |
elif dupes == ['']: | |
print('No input received') | |
sys.exit() | |
elif len(dupes) >= 6: | |
print('6 or greater trends at once is not supported.') | |
sys.exit() | |
print("Loading, please wait...") | |
raw = get(base) | |
if "We\'re sorry but there\'s been an error." in raw.text: | |
print("Looks like we've been throttled.") | |
sys.exit() | |
else: | |
info = raw.text.split('[],"rows"')[1].split("showHeadlines") | |
needed = info[0].encode('ascii','ignore')[1:-2] | |
loaded = load(needed) | |
print ('\n{0}\n'.format('-' * 50)) | |
for entry in (loaded): | |
output = str(entry[0]['f']) | |
for i, dupe in enumerate(dupes): | |
output = output + '{0}{1}:{2}'.format((" - " if i==0 else ", "), dupe, entry[i*4 + 3]) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment