Last active
April 23, 2020 18:13
-
-
Save DerekHawkins/098e8870ff96280bece580cffa9493fe 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
# Essentials | |
import pandas as pd | |
import numpy as np | |
from time import sleep | |
from random import randint | |
# Trends API | |
from pytrends.request import TrendReq | |
# Optional | |
from tqdm import notebook as tqdm | |
# For Data Visualization | |
import plotly.graph_objects as go | |
pytrends = TrendReq(hl='en-US', tz=360) | |
### Establish Keyword List and Build Payload ### | |
kw_list = [ | |
'Keywords go here' | |
] | |
frame = [] | |
for i in tqdm.tqdm(kw_list): | |
try: | |
pytrends.build_payload([i], cat=0, geo='US', gprop='') | |
interest = pytrends.get_historical_interest([i], year_start=2020, month_start=4, | |
day_start=12, hour_start=0, year_end=2020, | |
month_end=4, day_end=19, hour_end=0, cat=0, | |
geo='', gprop='', sleep=0) | |
gr = interest | |
hold = gr.columns[0] | |
name = gr.columns[0] | |
gr = gr.rename(columns={name: 'Search Popularity'}) | |
gr = gr[['Search Popularity']] | |
gr['Term'] = hold | |
sleep(randint(5,20)) | |
frame.append(gr) | |
except: | |
pass | |
final = pd.concat(frame) | |
final.index = pd.MultiIndex.from_arrays([final.index.date, final.index.time], names=['Date','Time']) | |
pivot = final.pivot_table(index=['Time'], | |
values=['Search Popularity'], | |
aggfunc=np.mean) | |
layout = go.Layout( | |
title='Aggregated Time of Day Demand: Office Supplies', | |
yaxis=dict(title='Search Popularity Index'), | |
xaxis=dict(title='Time of Day')) | |
fig = go.Figure(data=go.Scatter(x=pivot.index, y=pivot['Search Popularity']), | |
layout=layout) | |
fig.show() | |
final.to_csv('raw_data.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment