def getGoogleKeywords(kwlist): pytrend = TrendReq() # keyword_codes is only to save keywords that passed to google api KEYWORDS_CODES = [] # result returns a dictionary where key is the converted google-keyword code, value is the orginal keyword result = {} for kw in kwlist: if (len(kw.split(" ")) > 1): try: if (len(pytrend.suggestions(keyword=kw)) > 0): KEYWORDS_CODES.append(pytrend.suggestions(keyword=kw)[0]) result[pytrend.suggestions(keyword=kw)[0]["mid"]] = kw else: KEYWORDS_CODES.append({"mid": kw, "title": kw, "type": ""}) result[kw] = kw except Exception as e: print(e) else: KEYWORDS_CODES.append({"mid": kw, "title": kw, "type": ""}) result[kw] = kw # KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS] df_CODES = pd.DataFrame(KEYWORDS_CODES) EXACT_KEYWORDS = df_CODES['mid'].to_list() # Individual_EXACT_KEYWORD = list(zip(*[iter(EXACT_KEYWORDS)]*1)) # Individual_EXACT_KEYWORD = [list(x) for x in Individual_EXACT_KEYWORD] # print(EXACT_KEYWORDS) # print("the drugname-searchedKyword dictionary is: ") # print(result) return df_CODES, result
def three_search(m1, m2, m3): search = [] pytrend = TrendReq() search.append(m1) search.append(m2) search.append(m3) pytrend.build_payload(search, geo='IN', timeframe='today 3-m') result_total = pytrend.interest_over_time() result_total_geo = pytrend.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False) index_list = list(map(lambda x: str(x), result_total.tail(30).index)) index_list = list( map( lambda x: str( datetime.strptime(x, "%Y-%m-%d %H:%M:%S").strftime("%d-%b")), index_list)) m1_3m = list(result_total[m1].tail(30)) m2_3m = list(result_total[m2].tail(30)) m3_3m = list(result_total[m3].tail(30)) index_list_geo_1 = list(result_total_geo[m1].nlargest(10).index) index_list_geo_2 = list(result_total_geo[m2].nlargest(10).index) index_list_geo_3 = list(result_total_geo[m3].nlargest(10).index) m1_geo = list(result_total_geo[m1].nlargest(10).values) m2_geo = list(result_total_geo[m2].nlargest(10).values) m3_geo = list(result_total_geo[m3].nlargest(10).values) m1_key = list(pd.DataFrame(pytrend.suggestions(m1))["title"].values) m2_key = list(pd.DataFrame(pytrend.suggestions(m2))["title"].values) m3_key = list(pd.DataFrame(pytrend.suggestions(m3))["title"].values) size = len(search) return (m1_3m, m2_3m, m3_3m, index_list, m1, m2, m3, size, m1_geo, m2_geo, m3_geo, index_list_geo_1, index_list_geo_2, index_list_geo_3, m1_key, m2_key, m3_key)
class Trends: def __init__(self, keywords): self.pytrends = TrendReq(hl='uk', tz=360) self.pytrends.build_payload(keywords) def interest_over_time(self): return self.pytrends.interest_over_time() def interest_by_region(self, resolution='COUNTRY', inc_low_vol=False): return self.pytrends.interest_by_region(resolution=resolution, inc_low_vol=inc_low_vol) def related_topics(self): return self.pytrends.related_topics() def related_querries(self): return self.pytrends.related_queries() def trending_searches(self, pn='ukraine'): return self.pytrends.trending_searches(pn=pn) def top_charts(self, date, hl='uk', tz=360, geo='GLOBAL'): return self.pytrends.top_charts(date, hl=hl, tz=tz, geo=geo) def suggestions(self, keyword): return self.pytrends.suggestions(keyword)
def create_mid_from_keyword(keyword): pytrend = TrendReq() # getMid mid_of_keyword = pd.DataFrame(pytrend.suggestions(keyword[0])) print("---------------- AVAILABLE MID >>> ---------------") print(mid_of_keyword) print("-------------------------------\n") if not mid_of_keyword.empty: mids_of_desired_types = mid_of_keyword[ (mid_of_keyword['type'].str.lower().str.contains('bank')) | (mid_of_keyword['type'].str.lower().str.contains('company')) | (mid_of_keyword['type'].str.lower().str.contains('corporation'))] if mids_of_desired_types['mid'].values.any(): mid_of_interest = [ mids_of_desired_types['mid'].values[0], mids_of_desired_types['title'].values[0], mids_of_desired_types['type'].values[0] ] print("---------------- MID OF INTEREST >>> ---------------") print(mid_of_interest) print( "---------------- <<< MID OF INTEREST ---------------\n\n\n") with open("results.csv", "a") as resultsFile: mid_entry = keyword[0] + ',' + mid_of_interest[ 0] + ',' + mid_of_interest[1] + ',' + mid_of_interest[ 2] + "\n" resultsFile.write(mid_entry)
def get_pytrend_info(initlist, startdate, enddate, keyword, catinfo, suggestion_id=-1, isfirst=False): #pytrends = TrendReq(hl='en-US',tz=360) pytrends = TrendReq(hl='ko', tz=540) #pytrends = TrendReq(hl='en-US', tz=360, proxies = {'https': 'https://34.203.233.13:80'}) if (suggestion_id != -1): # Get suggestion keywords from original keyword if required (suggestion_id is not -1) keywords_suggestions = pytrends.suggestions(keyword=keyword[0]) if (len(keywords_suggestions) is 0): # Case could not find keyword suggestion keywords_list = keyword else: # Case find keyword suggestion, with using suggestion id keywords_list = list() keywords_list.append(keywords_suggestions[0].get("mid")) else: # If suggestion is not required (suggestion_id is -1) keywords_list = keyword #print("Keyword :: " + str(keywords_list)) ## options for build_payload # cat : category (number (0 = all, ...)) # SEE categories https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories # geo : Conuntry (United state = 'US'), defaults to world all # tz : timezone offset (??) # timeframe : format should be YYYY-MM-DD YYYY-MM-DD" # gprop : google property (images, news, youtubes) pytrends.build_payload(kw_list=keywords_list, cat=catinfo, timeframe=str(startdate + " " + enddate), geo='KR', gprop='') getdatainfo = pytrends.interest_over_time() # Delete 'isPartial' column #del getdatainfo['isPartial'] print(getdatainfo) data_list = np.array(getdatainfo) # reverse order data_list = np.flipud(data_list) end_date = datetime.datetime.strptime(enddate, "%Y-%m-%d") decrease_date = datetime.timedelta(days=1) initlist = append_data_into_list(initlist, data_list, end_date, decrease_date, isfirst) return initlist, len(data_list), data_list
def suggestions(): a = request.data print(a) pie = TrendReq(hl='en-US', tz=360) keyword = a print("\n Recommendation for :", keyword, "\n") df = pie.suggestions(keyword) return jsonify(df)
def test_request_args_passing(self): requests_args = { 'headers': { 'User-Agent': 'pytrends', } } pytrend = TrendReq(requests_args=requests_args) pytrend.build_payload(kw_list=['bananas']) self.assertIsNotNone(pytrend.suggestions('bananas')) self.assertIsNotNone(pytrend.trending_searches())
def get_suggestions_and_topics(participant_name, hl='en-US', geo="", gprop=""): pytrends = TrendReq(hl=hl, tz=360, geo=geo) pytrends.build_payload([participant_name], cat=0, timeframe='2017-06-01 2020-06-01', geo=geo, gprop=gprop) dict_related_topic = pytrends.related_topics() suggestions = pytrends.suggestions(participant_name) return dict_related_topic, suggestions
class Trends: ''' Wrapper class for pytrends library. ''' def __init__(self, keywords): ''' Initialize trends object. ''' self.pytrends = TrendReq(hl='uk', tz=360) self.pytrends.build_payload(keywords) def interest_over_time(self): ''' Get interest overt time. ''' return self.pytrends.interest_over_time() def interest_by_region(self, resolution='COUNTRY', inc_low_vol=False): ''' Get interest by region. ''' return self.pytrends.interest_by_region(resolution=resolution, inc_low_vol=inc_low_vol) def related_topics(self): ''' Get related topics. ''' return self.pytrends.related_topics() def related_querries(self): ''' Get related search querries. ''' return self.pytrends.related_queries() def trending_searches(self, pn='ukraine'): ''' Get trending searhes by country. ''' return self.pytrends.trending_searches(pn=pn) def top_charts(self, date, hl='uk', tz=360, geo='GLOBAL'): ''' Get top charts by date. ''' return self.pytrends.top_charts(date, hl=hl, tz=tz, geo=geo) def suggestions(self, keyword): ''' Get suggestions for keyword. ''' return self.pytrends.suggestions(keyword)
def _initialize(searchwords, date_start, date_stop, geo, method, cat=0, include_suggestions=False, verbose=3): # https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories # cat=0 : All categories # cat=47 : Autos & Vehicles # method : Can be 'images', 'news', 'youtube' or 'froogle' (for Google Shopping results) # Convert name to code if geo is None: geo = '' if len(geo) > 3: geo = worldmap.county2code(geo)[0][0] geo = geo.upper() # Set dates date_start, date_stop, date_range = _set_dates(date_start, date_stop, verbose=verbose) # Set up the trend fetching object pytrends = TrendReq(hl='en-US', tz=360) # Include google suggestions if include_suggestions: suggested_words = [] for word in searchwords: suggested_words = suggested_words + list( pd.DataFrame(pytrends.suggestions(word))['title'].values) searchwords = searchwords + suggested_words if verbose >= 3: print('[googletrends] [%d] suggestions included.' % (len(suggested_words))) if date_start != date_stop: if geo == '': pytrends.build_payload(searchwords, cat=cat, timeframe=date_range[0], gprop=method) else: pytrends.build_payload(searchwords, geo=geo, cat=cat, timeframe=date_range[0], gprop=method) else: raise Exception('Start and stop time must be different.') # pytrends.build_payload(searchwords, geo=geo, cat=0, timeframe=timeframe, gprop=method) return (pytrends, geo)
def getSearchQueries(searchWord): pytrends = TrendReq(hl='en-US', tz=360) kw_list = [searchWord] pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='', gprop='') df = pytrends.related_queries() et = pytrends.suggestions(searchWord) print(et) print(df)
def show_list_of_keyword(keyword): #pytrends = TrendReq(hl='en-US',tz=360) pytrends = TrendReq(hl='ko', tz=540) #pytrends = TrendReq(hl='en-US', tz=360, proxies = {'https': 'https://34.203.233.13:80'}) if (len(keyword) > 0): keywords_list = keyword[0] else: keywords_list = keyword # print related suggestion print(pytrends.suggestions(keyword=keywords_list)) return
def getSuggestions(keyword): """ Parameters: Keyword (str): Keyword to look for suggestions for Returns: df: Dateframe of suggestions, where mid is keyword ID for associated type """ pytrends = TrendReq(hl='en-US', tz=360) suggestions = (pytrends.suggestions(keyword)) suggestions = pd.DataFrame(suggestions) return suggestions
def get_cat_suggestions(kw_list_obj): # kw_list_obj = 'snowden' kw_list = [kw_list_obj] # kw_list = [kw_list_obj] print " get_cat_suggestions task key words :" + str(kw_list) # Login to Google. Only need to run this once, the rest of requests will use the same session. # pytrend = TrendReq(google_username, google_password, hl='en-US', custom_useragent='My Pytrends Script') pytrend = TrendReq() pytrend.build_payload(kw_list, timeframe='today 5-y') suggestions_dict = pytrend.suggestions(keyword=kw_list_obj) return suggestions_dict ## example code # google_username = "******" # google_password = "******" # path = "." # # terms = [ # "Image Processing", # "Signal Processing", # "Computer Vision", # "Machine Learning", # "Information Retrieval", # "Data Mining" # ] # # connect to Google Trends API # connector = pyGTrends(google_username, google_password) # # # for label in terms: # print(label) # sys.stdout.flush() # #kw_string = '"{0}"'.format(keyword, base_keyword) # connector.request_report(label, geo="US", date="01/2014 96m") # # wait a random amount of time between requests to avoid bot detection # time.sleep(randint(5, 10)) # # download file # connector.save_csv(path, label) # # for term in terms: # data = connector.get_suggestions(term) # pprint(data)
def pytrends_query(keyword, language): if language == "Polski": lang = "pl" elif language == "Angielski": lang = "en" else: lang = "Polski" pytrend = TrendReq(hl=lang) # Get Google Keyword Suggestions keywords = pytrend.suggestions(keyword=keyword) for elem in keywords: elem.pop('mid', None) return keywords
def excelWriter(): # get the user-input variable x1 = entry1.get() canvas1.create_window(200, 210) # get our Google Trends data pytrend = TrendReq() kws = pytrend.suggestions(keyword=x1) df = pd.DataFrame(kws) df = df.drop(columns='mid') # create excel writer object writer = pd.ExcelWriter('keywords.xlsx') # pylint: disable=abstract-class-instantiated df.to_excel(writer) writer.save() # open your excel file os.system("keywords.xlsx") print(df)
def gtrend_getvalue(kw_list,output_file,timeframe): """ ライブラリを使用してGoogleTrendsからデータを取得する。 #pytrends ref https://pypi.org/project/pytrends/#interest-by-region """ try: sp = kw_list[0] pytrends = TrendReq(hl='ja-JP', tz=360) pytrends.build_payload(kw_list, cat=0, timeframe=timeframe, geo='JP', gprop='') #関連キーワード trendsdata = pytrends.related_queries() o = output_file s = sp + 'query' exportdata(trendsdata,o,s,1) #関連トピック trendsdata = pytrends.related_topics() s = sp + 'topic' exportdata(trendsdata,o,s,1) #地域別の関心 trendsdata = pytrends.interest_by_region(resolution='REGION', inc_low_vol=True, inc_geo_code=False) s = sp + 'region' exportdata(trendsdata,o,s,0) #時系列 trendsdata = pytrends.interest_over_time() s = sp + 'overtime' exportdata(trendsdata,o,s,0) #サジェスト trendsdata = pytrends.suggestions(sp) s = sp + 'suggestions' suggest_to_excel(trendsdata,o,s) #注目キーワード #trendsword = pytrends.trending_searches(pn='united_states') #アメリカ #trendsword = pytrends.trending_searches(pn='japan') #日本 #s = "trendword" #f = exportdata(trendsword,o,s,0) except Exception as e: t, v, tb = sys.exc_info() print(traceback.format_exception(t,v,tb)) print(traceback.format_tb(e.__traceback__))
def excelWriter(): # get the user-input variable user_input = entry1.get() canvas1.create_window(200, 210) # get our Google Trends data pytrend = TrendReq() suggested_keywords = pytrend.suggestions(keyword=user_input) # Dataframe df = pd.DataFrame(suggested_keywords) df = df.drop(columns='mid') # create excel writer object writer = pd.ExcelWriter('keywords.xlsx') df.to_excel(writer) writer.save() # open your excel file os.system("keywords.xlsx") print(df)
class Trends: def __init__(self): self.pytrends = TrendReq() def get_results(self, keyword): trends = [] try: suggs = self.pytrends.suggestions(keyword) kw_list = [suggs[0]['mid']] self.pytrends.build_payload(kw_list, cat=0, timeframe='today 5-y', geo='', gprop='') df = self.pytrends.related_topics() for i in df[suggs[0]['mid']]['top']['topic_title'][:3]: if i.lower() != keyword.lower(): trends.append(i.lower()) except: pass return trends
class Example(QWidget): overlay: QImage # Overlay picture on window timer: QTimer # Time init painter: QPainter Colors: list arr_points: list DeltaX: int DeltaY: int SelfHeight: int LastAngle: int stationSize: list LastMousePosition: QPoint AllData: pd.DataFrame ActialDates: pd.DataFrame WithDates: pd.DataFrame toolTipWidget: QLabel pytrends: TrendReq def __init__(self): super().__init__() self.initUI() def keyPressEvent(self, event): if event.key() == 16777216: # Esc # self.FileSaving(self.arr_points, FILE_NAME) sys.exit() #Programm Closing elif event.key() == 16777249: #Ctrl return # self.UpdateGoogleData() # self.FileSaving(self.GetGoogleData(stationsName), FILE_DATA_NAME) def UpdateGoogleData(self): self.pytrends = TrendReq(hl='ru-RU', tz=360) self.WithDates = pd.DataFrame(columns=['name', 'date', 'value']) for i, row in self.AllData.iterrows(): if i == 61: print('ha') if (not row['popularity_mean'] > 0 or IS_FULL_LOAD ) and not row['Line'] == '13' and not row['Line'] == '14': station_popularity_mean, station_popularity = self.GetStation_popularity( row['Name'], self.pytrends) print(' '.join([ str(i), row['Name'], str(round(station_popularity_mean, 2)) ])) self.AllData.at[i, 'popularity_mean'] = station_popularity_mean if len(station_popularity) > 0: new_row = pd.DataFrame.from_dict({ 'name': row['Name'], 'date': list(station_popularity[ station_popularity.columns[0]].index), 'value': list( station_popularity[station_popularity.columns[0]]), 'ID': row['ID'] }) self.WithDates = self.WithDates.append(new_row, ignore_index=True) #self.AllData.at[i,'popularity'] = ':'.join([str(item) for item in station_popularity]) else: print( str(i) + ' ' + row['Name'] + ' ' + str(round(row['popularity_mean'], 2)) + ' not loaded') # self.FileDrawing(False) QApplication.processEvents() # if i>10: # break # self.WithDates.to_csv(FILE_DATES_NAME, index = False, sep=',', encoding='utf-8-sig') if i % 50 == 0: self.AllData.to_csv(FILE_DATA_NAME, index=False, sep=',', encoding='utf-8-sig') self.WithDates.to_csv(FILE_DATES_NAME, index=False, sep=',', encoding='utf-8-sig') print('All Loaded!') def GetStation_popularity(self, station_name, trends_object): # timeframe='today 5-y' suggestions_dict = self.pytrends.suggestions(keyword=station_name + ' Станция метрополитена') # kw_list = station_name 'type:Moscow Metro'] suggestions = [ x for x in suggestions_dict if x['title'] == station_name ] if len(suggestions) > 0: kw_list = [suggestions[0]['mid']] print(' ' + suggestions[0]['type']) else: return 0, [] #kw_list = [station_name] #print(' no suggestions') trends_object.build_payload(kw_list, cat=0, geo='RU', timeframe=DATE_START + ' ' + DATE_END, gprop='') interest = trends_object.interest_over_time() # self.WithDates.set_index('date') # self.WithDates = self.WithDates.append(interest) mean = interest.mean() if len(list(mean)) > 0: return mean[0], interest else: return 0, [] def mousePressEvent(self, event): mousePoint = event.pos() self.LastMousePosition = mousePoint if event.button() == 1: # self.painter.setFont(QFont('Arial', 111)) # self.painter.drawText(QPoint(200,200), 'HELLO') WindowW = self.frameGeometry().width() # WindowSize WindowH = self.frameGeometry().height() # WindowSize imgH = self.overlay.height() # Original Picture Size imgW = self.overlay.width() # Original Picture Size img = self.overlay.scaledToHeight(self.SelfHeight, Qt.FastTransformation) # AdjX = (WindowW-img.width())/2 ResultX = imgW * (mousePoint.x() - self.DeltaX) / img.width() ResultY = imgH / 100 * ((mousePoint.y() - self.DeltaY) / (self.SelfHeight / 100)) # eraser = 7 # self.painter.drawEllipse(QPoint(ResultX, ResultY), RADIUS, RADIUS) for i, row in self.AllData.iterrows(): if ResultX >= row['X'] - (row['popularity_mean'] + ADDITIONAL) /2 and \ ResultX <= row['X'] + (row['popularity_mean'] + ADDITIONAL) /2 and \ ResultY >= row['Y'] - (row['popularity_mean'] + ADDITIONAL) /2 and \ ResultY <= row['Y'] + (row['popularity_mean'] + ADDITIONAL) /2: text = row['Name'] + ', ' + str( round(row['popularity_mean'], 1)) + '%' self.toolTipWidget.setText(text) self.toolTipWidget.move(event.pos()) self.toolTipWidget.adjustSize() self.toolTipWidget.show() # self.painter.eraseRect(QRect(QPoint(ResultX-RADIUS/2-eraser, ResultY-RADIUS/2-eraser), QPoint(ResultX+radius/2+eraser, ResultY+radius/2+eraser))) self.arr_points.append((ResultX, ResultY)) self.update() # Redraw if event.button() == 2: WindowW = self.frameGeometry().width() # WindowSize WindowH = self.frameGeometry().height() # WindowSize imgH = self.overlay.height() # Original Picture Size imgW = self.overlay.width() # Original Picture Size img = self.overlay.scaledToHeight(self.SelfHeight, Qt.FastTransformation) # AdjX = (WindowW-img.width())/2 ResultX = imgW * (mousePoint.x() - self.DeltaX) / img.width() ResultY = imgH / 100 * ((mousePoint.y() - self.DeltaY) / (self.SelfHeight / 100)) # eraser = 7 self.painter.drawEllipse(QPoint(ResultX, ResultY), RADIUS, RADIUS) # self.painter.eraseRect(QRect(QPoint(ResultX-RADIUS/2-eraser, ResultY-RADIUS/2-eraser), QPoint(ResultX+radius/2+eraser, ResultY+radius/2+eraser))) self.arr_points.append((ResultX, ResultY)) # print([(round(x[0]), round(x[1])) for x in self.arr_points]) self.update() #Redraw def mouseReleaseEvent(self, event): self.point = None self.toolTipWidget.hide() ''' def FileSaving(self, arr: list, fileName: str): with open(fileName, 'w') as f: for item in arr: f.write(','.join(str(x) for x in item) + '\n') f.close() def FileReading(self, fileName: str): with open(fileName, 'r') as f: names = f.read().split('\n') f.close() #print(names) return [x.split(',') for x in names] def GetGoogleData(self, namesSt: list): stationSizes = [] for station in namesSt: sz = self.GetStationpopularity_meanarity([station], self.pytrends) if sz>0: stationSizes.append([station, sz]) print(station+': '+str(round(sz, 1))) else: print(station+' error') return stationSizes ''' def FileDrawing(self, animate): if not animate: self.timer.stop() penLine = QPen(QColor(Qt.red)) penLine.setWidth(10) # self.ActialDates = self.WithDates[self.WithDates.date == '2018/09/02'] for i, row in self.AllData.iterrows(): penEllipse = QPen(self.Colors[int(row['Line'].replace('A', '')) - 1]) penEllipse.setWidth(5) x = row['X'] y = row['Y'] self.painter.setPen(penLine) self.painter.setPen(penEllipse) penLine = QPen(QColor(Qt.red)) self.painter.setBrush(QColor(Qt.white)) if row['popularity_mean'] > 0: CircleSize = (RADIUS / 100) * row['popularity_mean'] + ADDITIONAL else: CircleSize = 10 self.painter.drawEllipse( float(x) - CircleSize, float(y) - CircleSize, CircleSize * 2, CircleSize * 2) QApplication.processEvents() self.timer.start(1000 / UPDATE_SPEED) self.overlay.save('Picture Of Map.jpg', format='jpeg') def closeEvent(self, event): # self.FileSaving(self.arr_points, FILE_NAME) sys.exit() # Programm Closing def initUI(self): self.Colors = [ QColor(228, 37, 24), # 1 QColor(75, 175, 79), # 2 QColor(5, 114, 185), # 3 QColor(36, 188, 239), # 4 QColor(146, 82, 51), # 5 QColor(239, 128, 39), # 6 QColor(148, 63, 144), # 7 QColor(255, 209, 30), # 8 QColor(173, 172, 172), # 9 QColor(185, 206, 31), # 10 QColor(134, 204, 206), # 11 QColor(186, 200, 232), # 12 QColor(68, 143, 201), # 13 QColor(232, 68, 57), # 14 ] self.showMaximized() #self.showNormal() self.setStyleSheet("background-color: white;") self.toolTipWidget = QLabel() self.toolTipWidget.setStyleSheet("QLabel {" "border-style: solid;" "border-width: 1px;" "border-color: grey; " "border-radius: 3px;" "padding: 5px 5px 5px 5px;" "background-color: rgb(255,255,225);" "}") self.toolTipWidget.setWindowFlags(Qt.ToolTip) self.toolTipWidget.setAttribute(Qt.WA_TransparentForMouseEvents) self.toolTipWidget.hide() self.interest = None self.arr_points = [] self.ActialDates = [] self.stationSize = [] self.LastAngle = 0 self.timer = QTimer() # Timer init self.timer.timeout.connect(self.update) # Timer init self.setWindowTitle('Moscow Metro Map Poppularity') # Title self.point = None self.DeltaX = 0 self.DeltaY = 0 self.SelfHeight = self.frameGeometry().height() self.LastMousePosition = QPoint(0, 0) self.overlay = QImage() # self.overlay.load('Moscow Metro Map Stations popularity_meanarity\\MainMap.bmp') self.overlay.load(FILE_BACKGROUND) pen = QPen(QColor(Qt.red)) pen.setWidth(5) self.painter = QPainter(self.overlay) self.painter.setPen(pen) self.painter.Antialiasing = False self.timer.start(1000 / UPDATE_SPEED) # self.AllData = self.FileReading(FILE_DATA_NAME) self.AllData = pd.read_csv(FILE_DATA_NAME, ',', encoding='utf-8-sig') self.WithDates = pd.read_csv(FILE_DATES_NAME, ',', encoding='utf-8-sig') # print(self.WithDates) # self.AllData = [x for x in self.AllData.iterrows() if x['Line']='8A'] # self.arr_points = self.FileReading() # self.FileDrawing() def paintEvent(self, event): painter = QPainter() painter.begin(self) # windowsWidth = self.frameGeometry().width() windowsHeight = self.frameGeometry().height() img = self.overlay.scaledToHeight(self.SelfHeight, 0) painter.drawImage(self.DeltaX, self.DeltaY, img) painter.end() del painter def mouseMoveEvent(self, event): CurentPos = event.pos() self.DeltaX -= self.LastMousePosition.x() - CurentPos.x() self.DeltaY -= self.LastMousePosition.y() - CurentPos.y() # self.LastMousePosition = mousePoint self.LastMousePosition = event.pos() def wheelEvent(self, event): # print(str(event.angleDelta())) self.SelfHeight += (event.angleDelta().y()) / 10 self.LastAngle = event.angleDelta().y() def resizeEvent(self, event): self.SelfHeight = self.frameGeometry().height() - 10 if hasattr(self, 'overlay'): img = self.overlay.scaledToHeight(self.SelfHeight, 0) self.DeltaX = (self.frameGeometry().width() - img.width()) / 2 self.DeltaY = 0
print(df) # toprelated toprelated = pytrend.related(trend_payload, related_type='top') print(toprelated) risingrelated = pytrend.related(trend_payload, related_type='rising') print(risingrelated) # top30in30 top30in30 = pytrend.top30in30() print(top30in30) country_payload = {'geo': 'US'} # hottrends hottrends = pytrend.hottrends(country_payload) print(hottrends) # hottrendsdetail # returns XML data hottrendsdetail = pytrend.hottrendsdetail(country_payload) print(hottrendsdetail) payload = {'date': '201601', 'geo': 'US'} # alltopcharts topcharts = pytrend.topcharts(payload) print(topcharts) keyword = 'pizza' # suggestions suggestions = pytrend.suggestions(keyword) print(suggestions)
data # the keyword to extract data kw = "python" pt.build_payload([kw], timeframe="all") # get the interest by country ibr = pt.interest_by_region("COUNTRY", inc_low_vol=True, inc_geo_code=True) # sort the countries by interest ibr[kw].sort_values(ascending=False) # get related topics of the keyword rt = pt.related_topics() rt[kw]["top"] # get related queries to previous keyword rq = pt.related_queries() rq[kw]["top"] # get suggested searches pt.suggestions("python") # another example of suggested searches pt.suggestions("America") # trending searches per region ts = pt.trending_searches(pn="united_kingdom") ts # real-time trending searches pt.realtime_trending_searches()
# Login to Google. 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=['pizza', 'bagel']) # 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()) # 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 Top Charts top_charts_df = pytrend.top_charts(cid='actors', date=201611) print(top_charts_df.head()) # Get Google Keyword Suggestions suggestions_dict = pytrend.suggestions(keyword='pizza') print(suggestions_dict)
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()) # 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) # Get Google Realtime Search Trends realtime_searches = pytrend.realtime_trending_searches() print(realtime_searches.head())
def test_suggestions(self): pytrend = TrendReq() pytrend.build_payload(kw_list=["pizza", "bagel"]) self.assertIsNotNone(pytrend.suggestions(keyword="pizza"))
''' data = trends.trending_searches(pn="united_states") print(data.head(10)) data.to_csv('trends.csv') # Get Google Hot Trends data today_searches_df = trends.today_searches(pn='US') print(today_searches_df.head(20)) today_searches_df.to_csv('today_searches_df.csv') #Get Google Keyword Suggestion #Let’s get a suggestion for the keyword “diet” and “vegan“ # Get Google Keyword Suggestions suggestions_dict = trends.suggestions(keyword='diet') print(pd.DataFrame(suggestions_dict).drop('mid', axis=1)) suggestions_dict = trends.suggestions(keyword='vegan') print(pd.DataFrame(suggestions_dict).drop('mid', axis=1)) suggestions_dict.to_csv('suggestions_dict.csv') # Create payload and capture API tokens. Only needed for interest_over_time(), interest_by_region() & related_queries() trends.build_payload(kw_list=['coronavirus']) # Interest Over Time interest_over_time_df = trends.interest_over_time() print(interest_over_time_df.tail(10)) interest_over_time_df.to_csv('interest_over_time_df.csv') # Interest by Region
import pytrends import pandas as pd from pytrends.request import TrendReq pytrend = TrendReq() trend = pytrend.trending_searches('UK') trend keywords = pytrend.suggestions(keyword='Mercedes benz') df = pd.DataFrame(keywords) df.drop(columns='mid') pytrend.build_payload(kw_list=['Coronavirus']) related_queries = pytrend.related_queries() related_queries.values() dir(pytrend) world = pytrend.build_payload(kw_list=['Lewis Hamilton']) dd = pytrend.interest_by_region() dd.reset_index().plot(x='geoName', y='Lewis Hamilton', figsize=(120, 10), kind='bar') print('hello sophie')
class DesignerTrendsCollector(BuilderTrendsCollector): """DesignerTrendsCollector contains the specific implementation of `BuilderTrendsCollector`. `DesignerTrendsCollector` contains the specific implementation of `BuilderTrendsCollector` based on the external library `pytrends`. Args: BuilderTrendsCollector (class): Abstract class that provides the implementations of the properties and methods. """ def __init__( self, keyword_list: list, timeframe: str = "today 5-y", language: str = "en-US", category: int = 0, timezone: int = 360, country: str = "", property_filter="", **kwargs, ) -> None: """Initialization of DesignerTrendsCollector Args: keyword_list (list): Keyword-list with the items to search for. timeframe (str, optional): Time frame, respectively, period to search for. Defaults to "today 5-y". language (str, optional): Search language. Defaults to "en-US". category (int, optional): Define a specific [search category](https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories). Defaults to 0. timezone (int, optional): [Search timezone](https://developers.google.com/maps/documentation/timezone/overview). Defaults to 360. country (str, optional): The country, where to search for. Defaults to "". property_filter (str, optional): Property filer of the search; only in news, images, YouTube, shopping. Defaults to "". """ self.keyword_list = keyword_list self.timeframe = timeframe self.language = language self.category = category self.timezone = timezone self.country = country self.property_filter = property_filter self.pytrends = TrendReq(hl=self.language, tz=self.timezone, **kwargs) self.pytrends.build_payload( kw_list=self.keyword_list, cat=self.category, timeframe=self.timeframe, geo=self.country, gprop=self.property_filter, ) self.reset() def reset(self) -> None: """Reset the product to empty.""" self._product = TrendProduct() @property def trends(self) -> TrendProduct: """Return the trend results. Returns: TrendProduct: (class) TrendProduct contains the dictionary and the return value of it. """ product = self._product self.reset() return product def get_interest_over_time(self) -> None: """Request data from a interest over time search.""" self._product.add_product( key=self.get_interest_over_time, value=self.pytrends.interest_over_time(), ) def get_interest_by_region(self, resolution: str, **kwargs) -> None: """Request data from a interest by region search. Args: resolution (str): The resolution of the subregion. """ self._product.add_product( key=self.get_interest_by_region, value=self.pytrends.interest_by_region(resolution=resolution, **kwargs), ) def get_trending_searches(self, trend_country: str) -> None: """Request data from a search by country. Args: trend_country (str, optional): Name of the country of intrest. Defaults to "united_states". """ self._product.add_product( key=self.get_trending_searches, value=self.pytrends.trending_searches(pn=trend_country), ) def get_today_searches(self, today_country: str) -> None: """Request data from the daily search trends. Args: today_country (str): Name of the country of intrest. """ self._product.add_product( key=self.get_today_searches, value=self.pytrends.today_searches(pn=today_country), ) def get_top_charts(self, date: int, top_country: str) -> None: """Request data from a top charts search. Args: date (int): Year top_country (str): Name of the country of intrest. """ self._product.add_product( key=self.get_top_charts, value=self.pytrends.top_charts(date, hl=self.language, tz=self.timezone, geo=top_country), ) def get_related_topics(self) -> None: """Request data of a related topics based on the keyword.""" self._product.add_product(key=self.get_related_topics, value=self.pytrends.related_topics()) def get_related_queries(self) -> None: """Request data of a related queries based on the keyword.""" self._product.add_product( key=self.get_related_queries, value=self.pytrends.related_queries(), ) def get_suggestions(self) -> None: """Request data from keyword suggestion dropdown search.""" self._product.add_product( key=self.get_suggestions, value={ keyword: self.pytrends.suggestions(keyword=keyword) for keyword in self.keyword_list }, ) def get_categories(self) -> None: """Request available categories data for the current search.""" self._product.add_product( key=self.get_categories, value=self.pytrends.categories(), ) def get_historical_interest( self, year_start: int, month_start: int, day_start: int, hour_start: int, year_end: int, month_end: int, day_end: int, hour_end: int, **kwargs, ) -> None: """Request data from a hour-grided time search. Args: year_start (int): Starting year month_start (int): Starting month day_start (int): Starting day hour_start (int): Starting hour year_end (int): Final year month_end (int): Final month day_end (int): Final day hour_end (int): Final hour """ self._product.add_product( key=self.get_historical_interest, value=self.pytrends.get_historical_interest( keywords=self.keyword_list, year_start=year_start, month_start=month_start, day_start=day_start, hour_start=hour_start, year_end=year_end, month_end=month_end, day_end=day_end, hour_end=hour_end, cat=self.category, geo=self.country, gprop=self.property_filter, **kwargs, ), )
class Searchtrends(Preprocessor): def __init__(self, interval, start_time, end_time): """ Initialise shared parameters. :interval: the time interval at which the training data will be collected and batched :start_time: earliest point from which data will be collected, as a datetime object :end_time: final point at which data will be collected, as a datetime object """ self.pytrends = TrendReq(hl='en-US', tz=0) self.interval = interval self.start_time = start_time self.end_time = end_time def get_training_data(self, topic): """ Call API to collect target dataset over the defined time period. Returns fully formatted data as a dataframe and summarized into intervals. Note that training data here has not yet been split into data vs. targets :topic: this will be the API specific target. E.g. a reddit subreddit or GDAX currency pair """ # Get topic suggestion suggestion = self.pytrends.suggestions(topic)[0]['mid'] # For each time period chunk, call downloader data = pd.DataFrame() # Empty list to append data delta = timedelta(days=180) # Keep to 6 month periods to ensure daily intervals slice_start = self.start_time while slice_start != self.end_time: slice_end = min(slice_start + delta, self.end_time) print("downloading {} data from {} to {}".format(topic, slice_start, slice_end)) df = self.trend_downloader( topic=[topic], start=slice_start, end=slice_end, ) slice_start = slice_end data = data.append(df) return data def trend_downloader(self, topic, start, end): """ For a specific time slice, requests search trend data by region normalized to 100 and combines it :start: in datetime format :end: in datetime format """ timeframe = date_to_datestring(start) + " " + date_to_datestring(end) # Global data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='', gprop='') WW_data = self.pytrends.interest_over_time() WW_data.columns= ['Worldwide', 'isPartial'] data = WW_data['Worldwide'].to_frame() # US Data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='US', gprop='') US_data = self.pytrends.interest_over_time() US_data.columns= ['US', 'isPartial'] data = data.join(US_data['US']) # UK Data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='GB', gprop='') GB_data = self.pytrends.interest_over_time() GB_data.columns= ['GB', 'isPartial'] data = data.join(GB_data['GB']) # UK Data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='FR', gprop='') FR_data = self.pytrends.interest_over_time() FR_data.columns= ['FR', 'isPartial'] data = data.join(FR_data['FR']) # Germany Data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='DE', gprop='') DE_data = self.pytrends.interest_over_time() DE_data.columns= ['DE', 'isPartial'] data = data.join(DE_data['DE']) # Russia Data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='RU', gprop='') RU_data = self.pytrends.interest_over_time() RU_data.columns= ['RU', 'isPartial'] data = data.join(RU_data['RU']) # Korea Data self.pytrends.build_payload(topic, cat=0, timeframe=timeframe, geo='KR', gprop='') KR_data = self.pytrends.interest_over_time() KR_data.columns= ['KR', 'isPartial'] data = data.join(KR_data['KR']) return data def get_test_data(self, topic): """ Call API to collect data for 1 time period only starting from now. Returns fully formatted data in dataframe. Note that this function will be significantly simpler than get_training_data since there is no need to loop through multiple time periods and aggregate multiple API calls :topic: this will be the API specific target. E.g. a reddit subreddit or GDAX currency pair """ raise NotImplementedError("{} must override step()".format(self.__class__.__name__))
# -*- coding: utf-8 -*- """ Created on Fri Oct 2 12:56:57 2020 @author: Administrator """ import pandas as pd import pytrends from pytrends.request import TrendReq pytrend = TrendReq() KEYWORDS = ['Nike', 'The North Face', 'Vans', 'The Timberland Company'] KEYWORDS_CODES = [pytrend.suggestions(keyword=i)[0] for i in KEYWORDS] df_CODES = pd.DataFrame(KEYWORDS_CODES) EXACT_KEYWORDS = df_CODES['mid'].to_list() DATE_INTERVAL = '2020-09-01 2020-9-30' COUNTRY = ['US', 'GB', 'DE'] CATEGORY = 0 SEARCH_TYPE = '' Individual_EXACT_KEYWORD = list(zip(*[iter(EXACT_KEYWORDS)] * 1)) Individual_EXACT_KEYWORD = [list(x) for x in Individual_EXACT_KEYWORD] dicti = {} i = 1 for Country in COUNTRY: for keyword in Individual_EXACT_KEYWORD: pytrend.build_payload(kw_list=keyword, timeframe=DATE_INTERVAL, geo=Country,
def test_suggestions(self): pytrend = TrendReq() pytrend.build_payload(kw_list=['pizza', 'bagel']) self.assertIsNotNone(pytrend.suggestions(keyword='pizza'))
def test_suggestions(self): pytrend = TrendReq() pytrend.build_payload(kw_list=['pizza', 'bagel']) self.assertIsNotNone(pytrend.suggestions(keyword='pizza'))