# Google Trend in Python # Prg1 取得數據 # Google Trend in Python #!pip install pytrends from pytrends.request import TrendReq from pprint import pprint pytrend = TrendReq(hl='en-US', tz=360) # 設定要查詢的關鍵字 keywords_list = ['數位','行銷'] # 取得1個月的數據 pytrend.build_payload(kw_list=keywords_list,cat=0, timeframe='today 1-m', geo='TW', gprop='') pprint(pytrend.interest_over_time()) # 取得5年的數據 pytrend.build_payload(kw_list=keywords_list,cat=0, timeframe='today 5-y', geo='TW', gprop='') pprint(pytrend.interest_over_time()) # ---------------------------------------------------- # Prg2 from pytrends.request import TrendReq # Only need to run this once, the rest of requests will use the same session. pytrend = TrendReq() # Create payload and capture API tokens. Only needed for interest_over_time(), interest_by_region() & related_queries() pytrend.build_payload(kw_list=[' ', ' '], geo='') # Interest Over Time interest_over_time_df = pytrend.interest_over_time() print(interest_over_time_df.head()) # Interest by Region interest_by_region_df = pytrend.interest_by_region() #print(interest_by_region_df.head()) print(interest_by_region_df) # Related Queries, returns a dictionary of dataframes related_queries_dict = pytrend.related_queries() #print(related_queries_dict) # Get Google Hot Trends data trending_searches_df = pytrend.trending_searches() #print(trending_searches_df.head()) # Get Google Hot Trends data today_searches_df = pytrend.today_searches() #print(today_searches_df.head()) # Get Google Top Charts top_charts_df = pytrend.top_charts(2018, hl='en-US', tz=300, geo='GLOBAL') #print(top_charts_df.head()) # Get Google Keyword Suggestions suggestions_dict = pytrend.suggestions(keyword='pizza') print(suggestions_dict) # --------------------------------------------- # Google Trnd資料抓取 #!pip install pytrends from pytrends.request import TrendReq pList = [] for keywords in data['blockchain','marketing]: print(keywords) keywords_list = [keywords] sleep_sec = randint(3,5) timesleep(sleep_sec) pytrend.build_payload(keywords_list, cat=0, timeframe='today 5-y', geo ='TW', gprop='') interest_over_time_df = pytrend.interest_over_time() pList.append(keywrods) for data in range(total_data_time): if len(interest_over_time_df.iloc[ : ]) == 0: pList.append(0) else: pList.append(interest_over_time_df.iloc[data,0]) print('Done')