from datetime import date, timedelta
from functools import partial
from time import sleep
from calendar import monthrange
import pandas as pd
from pytrends.exceptions import ResponseError
from pytrends.request import TrendReq
def convert_dates_to_timeframe(start: date, stop: date) -> str:
return f"{start.strftime('%Y-%m-%d')} {stop.strftime('%Y-%m-%d')}"
def _fetch_data(pytrends, build_payload, timeframe: str) -> pd.DataFrame:
attempts, fetched = 0, False
while not fetched:
try:
build_payload(timeframe=timeframe)
except ResponseError as err:
print(err)
print(f'Trying again in {60 + 5 * attempts} seconds.')
sleep(60 + 5 * attempts)
attempts += 1
if attempts > 3:
print('Failed after 3 attemps, abort fetching.')
break
else:
fetched = True
return pytrends.interest_over_time()
def get_daily_data(word: str,
start_date,
stop_date,
geo: str = '',
tz: int = 420):
pytrends = TrendReq(hl='zh-CN', tz=tz)
build_payload = partial(pytrends.build_payload,
kw_list=[word], cat=0, geo=geo, gprop='')
monthly = _fetch_data(pytrends, build_payload,
convert_dates_to_timeframe(start_date, stop_date))
monthly.to_sql()
print(monthly)
def run(keyword):
start = date(2022, 1, 1)
stop = date(2022, 7, 31)
data = get_daily_data(keyword, start, stop)
data.to_excel(keyword.replace('/', '').replace(':', '') + ".xlsx")
if __name__ == '__main__':
#pytrends = TrendReq(hl='zh-CN', tz=360)
#print(pytrends.suggestions("TSLA"))
#run("NASDAQ:TSLA")
run(TrendReq(hl='zh-CN', tz=420).suggestions("TSLA")[0]['mid'])
google trends 抓数据
最新推荐文章于 2025-03-28 09:51:05 发布