Skip to content

Commit 4c7ad69

Browse files
committed
move get_historical to TrendReq class
1 parent f7cd0d6 commit 4c7ad69

File tree

2 files changed

+44
-51
lines changed

2 files changed

+44
-51
lines changed

pytrends/get_historical_interest.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

pytrends/request.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import pandas as pd
77
import requests
88

9+
from datetime import datetime, timedelta
10+
import time
11+
912
from pytrends import exceptions
1013

1114
if sys.version_info[0] == 2: # Python 2
@@ -370,3 +373,44 @@ def categories(self):
370373
)
371374
return req_json
372375

376+
def get_historical_interest(self, keywords, 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):
377+
"""Gets historical hourly data for interest by chunking requests to 1 week at a time (which is what Google allows)"""
378+
379+
# construct datetime obejcts - raises ValueError if invalid parameters
380+
start_date = datetime(year_start, month_start, day_start, hour_start)
381+
end_date = datetime(year_end, month_end, day_end, hour_end)
382+
383+
# the timeframe has to be in 1 week intervals or Google will reject it
384+
delta = timedelta(days=7)
385+
386+
df = pd.DataFrame()
387+
388+
date_iterator = start_date
389+
date_iterator += delta
390+
391+
while True:
392+
if (date_iterator > end_date):
393+
# has retrieved all of the data
394+
break
395+
396+
# format date to comply with API call
397+
start_date_str = start_date.strftime('%Y-%m-%dT%H')
398+
date_iterator_str = date_iterator.strftime('%Y-%m-%dT%H')
399+
tf = start_date_str + ' ' + date_iterator_str
400+
401+
try:
402+
self.build_payload(keywords,cat, tf, geo, gprop)
403+
week_df = self.interest_over_time()
404+
df = df.append(week_df)
405+
except Exception as e:
406+
print(e)
407+
pass
408+
409+
start_date += delta
410+
date_iterator += delta
411+
412+
# just in case you are rate-limited by Google. Recommended is 60 if you are.
413+
if sleep > 0:
414+
time.sleep(sleep)
415+
416+
return df

0 commit comments

Comments
 (0)