From f96bd0aeb76f11f67ddb90be35fd1474a913c42a Mon Sep 17 00:00:00 2001 From: Thea Pilkati <53376757+tpilkati@users.noreply.github.com> Date: Wed, 24 Jun 2020 18:20:34 +0200 Subject: [PATCH 1/2] removed keywords restriction in payloads removed keywords restriction in order to look for most searched topics and queries during a certain period of time without having specified any keyword, as in: https://trends.google.com/trends/explore?date=today%201-m&geo=US --- pytrends/request.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/pytrends/request.py b/pytrends/request.py index 8e7d48c1..7ed91607 100644 --- a/pytrends/request.py +++ b/pytrends/request.py @@ -142,7 +142,7 @@ def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs): 'response with code {0}.'.format(response.status_code), response=response) - def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='', + def build_payload(self, kw_list = [], cat=0, timeframe='today 5-y', geo='', gprop=''): """Create the payload for related queries, interest over time and interest by region""" if gprop not in ['', 'images', 'news', 'youtube', 'froogle']: @@ -155,10 +155,15 @@ def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='', 'req': {'comparisonItem': [], 'category': cat, 'property': gprop} } - # build out json for each keyword - for kw in self.kw_list: - keyword_payload = {'keyword': kw, 'time': timeframe, - 'geo': self.geo} + # build out json for each keyword if there are keywords + if len(kw_list) > 0: + for kw in self.kw_list: + keyword_payload = {'keyword': kw, 'time': timeframe, + 'geo': self.geo} + self.token_payload['req']['comparisonItem'].append(keyword_payload) + # build out json if there are no keywords + else: + keyword_payload = {'keyword': '', 'time': timeframe, 'geo': self.geo} self.token_payload['req']['comparisonItem'].append(keyword_payload) # requests will mangle this if it is not a string self.token_payload['req'] = json.dumps(self.token_payload['req']) @@ -307,9 +312,6 @@ def related_topics(self): related_payload = dict() result_dict = dict() for request_json in self.related_topics_widget_list: - # ensure we know which keyword we are looking at rather than relying on order - kw = request_json['request']['restriction'][ - 'complexKeywordsRestriction']['keyword'][0]['value'] # convert to string as requests will mangle related_payload['req'] = json.dumps(request_json['request']) related_payload['token'] = request_json['token'] @@ -343,7 +345,7 @@ def related_topics(self): # in case no rising topics are found, the lines above will throw a KeyError df_rising = None - result_dict[kw] = {'rising': df_rising, 'top': df_top} + result_dict = {'rising': df_rising, 'top': df_top} return result_dict def related_queries(self): @@ -356,9 +358,6 @@ def related_queries(self): related_payload = dict() result_dict = dict() for request_json in self.related_queries_widget_list: - # ensure we know which keyword we are looking at rather than relying on order - kw = request_json['request']['restriction'][ - 'complexKeywordsRestriction']['keyword'][0]['value'] # convert to string as requests will mangle related_payload['req'] = json.dumps(request_json['request']) related_payload['token'] = request_json['token'] @@ -390,7 +389,7 @@ def related_queries(self): # in case no rising queries are found, the lines above will throw a KeyError rising_df = None - result_dict[kw] = {'top': top_df, 'rising': rising_df} + result_dict = {'top': top_df, 'rising': rising_df} return result_dict def trending_searches(self, pn='united_states'): From 6ca4733c1a23e45b6cfc084381466dcbd89e3d2b Mon Sep 17 00:00:00 2001 From: Thea Pilkati <53376757+tpilkati@users.noreply.github.com> Date: Fri, 14 Aug 2020 21:55:48 +0200 Subject: [PATCH 2/2] Updated request.py added related_topics and related_queries with no keywords as optional --- pytrends/request.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pytrends/request.py b/pytrends/request.py index 7ed91607..874e5dc9 100644 --- a/pytrends/request.py +++ b/pytrends/request.py @@ -304,7 +304,6 @@ def interest_by_region(self, resolution='COUNTRY', inc_low_vol=False, def related_topics(self): """Request data from Google's Related Topics section and return a dictionary of dataframes - If no top and/or rising related topics are found, the value for the key "top" and/or "rising" will be None """ @@ -344,13 +343,17 @@ def related_topics(self): except KeyError: # in case no rising topics are found, the lines above will throw a KeyError df_rising = None - - result_dict = {'rising': df_rising, 'top': df_top} + if len(self.kw_list) > 0: + # ensure we know which keyword we are looking at rather than relying on order + kw = request_json['request']['restriction'][ + 'complexKeywordsRestriction']['keyword'][0]['value'] + result_dict[kw] = {'rising': df_rising, 'top': df_top} + else: + result_dict = {'rising': df_rising, 'top': df_top} return result_dict def related_queries(self): """Request data from Google's Related Queries section and return a dictionary of dataframes - If no top and/or rising related queries are found, the value for the key "top" and/or "rising" will be None """ @@ -388,8 +391,13 @@ def related_queries(self): except KeyError: # in case no rising queries are found, the lines above will throw a KeyError rising_df = None - - result_dict = {'top': top_df, 'rising': rising_df} + if len(self.kw_list) > 0: + # ensure we know which keyword we are looking at rather than relying on order + kw = request_json['request']['restriction'][ + 'complexKeywordsRestriction']['keyword'][0]['value'] + result_dict[kw] = {'top': top_df, 'rising': rising_df} + else: + result_dict = {'top': top_df, 'rising': rising_df} return result_dict def trending_searches(self, pn='united_states'):