def bubble_data():
    pytrends = TrendReq(hl='en-US', tz=360)

    initial_keywords = ['obesity', 'diet', 'exercise', 'disease', 'cancer']
    keywords = {}
    pytrends.build_payload(kw_list=initial_keywords)

    print('Fecthing Related Terms')

    related = pytrends.related_queries()
    related_data = [related[x]['top'][:5].to_numpy() for x in related]
    related_terms = [x[:, 0] for x in related_data]

    print('Related Terms obtained')

    for x in range(len(initial_keywords)):
        keywords[initial_keywords[x]] = related_terms[x].tolist()

    print('Getting related Terms')
    for x in keywords.copy():
        subkeywords = keywords[x]
        pytrends.build_payload(kw_list=subkeywords)
        related = pytrends.related_queries()
        related_data = [related[x]['top'][:3].to_numpy() for x in related]
        related_terms = [x[:, 0] for x in related_data]
        for i in range(len(subkeywords)):
            keywords[x].extend(related_terms[i].tolist())
    print('Related terms obtained')

    data = {}

    for keyword in keywords:
        subkeywords = keywords[keyword]
        data[keyword] = {}

        chunkedList = list()
        for index in range(5, len(subkeywords) + 1, 5):
            chunkedList.append(subkeywords[index - 5:index])

        for currList in chunkedList:
            if len(currList) > 0:
                currList = list(dict.fromkeys(currList))
                print(currList)
                pytrends.build_payload(kw_list=currList)
                interest = pytrends.interest_over_time()

                for subkey in currList:
                    interest_data = {
                        interest.index[x].strftime("%m/%d/%Y"):
                        int(interest[subkey][x])
                        for x in range(len(interest))
                    }
                    data[keyword][subkey] = interest_data
                    data[keyword][subkey]['start_date'] = interest.index[
                        0].strftime("%m/%d/%Y")
                    data[keyword][subkey]['end_date'] = interest.index[
                        len(interest) - 1].strftime("%m/%d/%Y")
예제 #2
0
def search_generate(seed_keyword, top=True):
    pytrends = TrendReq()
    pytrends.build_payload([seed_keyword])
    if top:
        return pytrends.related_queries()[seed_keyword]['top']['query'].tolist(
        )
    else:
        return pytrends.related_queries(
        )[seed_keyword]['rising']['query'].tolist()
예제 #3
0
class GoogleTrendAPI():
    def __init__(self):
        self.pytrend = TrendReq()
        self.result = ''
        self.none_type_dic = ''

    def get_interest_over_time(self, cel):
        time.sleep(1)
        self.pytrend.build_payload(kw_list=[cel.celebCode],
                                   timeframe=cel.totalPeriod(),
                                   geo=cel.convertAlpha())
        time.sleep(2)
        try:
            time_result = self.pytrend.interest_over_time()[cel.celebCode]
        except:
            time_result = pd.DataFrame({cel.celebCode: [cel.celebName]})
            pass
        return time_result

    def get_related_queries(self, cel):
        self.pytrend.build_payload(kw_list=[cel.celebCode],
                                   timeframe=cel.totalPeriod(),
                                   geo=cel.convertAlpha())
        time.sleep(1)
        if self.pytrend.related_queries()[cel.celebCode]['rising'] is None:
            self.none_type_dic = pd.DataFrame({'query': [cel.celebName]})
            self.result = self.none_type_dic
        else:
            query_len = len(
                self.pytrend.related_queries()[cel.celebCode]['rising'])
            if query_len >= 3:
                self.result = self.pytrend.related_queries()[
                    cel.celebCode]['rising'].head(3)

            else:
                self.result = self.pytrend.related_queries()[
                    cel.celebCode]['rising'].head(query_len)

        return self.result

    def add_slash_query(self, related_query):
        query_array = []
        for i in tqdm(range(len(related_query))):
            sub_query = []
            for idx, row in related_query[i].iterrows():
                sub_query.append(row['query'])
            query_array.append('/'.join(sub_query))
        return query_array
예제 #4
0
def post_message(keyword):
    pytrends = TrendReq(hl='ja-JP', tz=540)
    pytrends.build_payload([keyword], timeframe='now 4-H', geo='JP')

    trends = list(pytrends.related_queries().values())[0]
    top_queries = trends.get('top')
    rising_queries = trends.get('rising')

    text = u''

    if rising_queries is not None and not rising_queries.empty:
        text += u'[%s] Rising queries\n' % keyword
        for index, top_query in rising_queries.iterrows():
            text += u'%2s. %3s%% \u2191  %s\n' % (index + 1, top_query.value,
                                                  top_query.query)
        text += u'\n\n'

    if top_queries is not None and not top_queries.empty:
        text += u'[%s] Top queries\n' % keyword
        for index, top_query in top_queries.iterrows():
            text += u'%2s. %3s%%  %s\n' % (index + 1, top_query.value,
                                           top_query.query)

            slack_data = {
                'text': text,
                'channel': CHANNEL,
                'username': USERNAME
            }

    requests.post(WEBHOOK_URL,
                  data=json.dumps(slack_data),
                  headers={'Content-Type': 'application/json'})
예제 #5
0
    def parse(self, response):
        try:
            pytrends = TrendReq(hl='en-US', tz=360)
            pytrends.build_payload(self.search_term.split('|||'),
                                   cat=self.cat,
                                   timeframe='{} {}'.format(
                                       self.start_date, self.end_date),
                                   geo=self.geo,
                                   gprop=self.gprop)
            df = pytrends.related_queries()

            keys = ['rising', 'top']
            for key in keys:
                json_data = json.loads(
                    df[self.search_term][key].to_json(orient="table"))

                for item in json_data['data']:
                    for search_term in self.search_term.split('|||'):
                        yield RelatedQueriesItem(
                            search_term=search_term,
                            search_phrase=item['query'],
                            top=item['value'] if key == 'top' else '',
                            breakout=item['value'] if key == 'rising' else '')
        except Exception as e:
            print(e)
            pass
예제 #6
0
def get_gtrend(code):
    all_edinetcodeinfo = EdinetCodeInfo.query.filter(EdinetCodeInfo.securitiescode==code).all()
    
    for edinetcodeinfo in all_edinetcodeinfo:
        gtrend = edinetcodeinfo.submitter.replace('株式会社','').replace('株','')
    pytrends = TrendReq(hl='ja-JP', tz=360)
    keyword=''.join(gtrend.split())
    kw_list = [keyword]
    pytrends.build_payload(kw_list, timeframe='today 1-m', geo='JP')
    df = pytrends.interest_over_time()
    dfi = df.drop("isPartial",axis=1)
    dt=[]
    for d in dfi.index.values:
        dt.append(str(d)[0:10])
    dfi['date'] = dt
    dfi.columns = ['name','date']
    
    trends = pytrends.related_queries()
    if(trends[keyword]['top'] is None):
        top = ['No data']
    else:
        top = trends[keyword]['top'].values.tolist()
    if(trends[keyword]['rising'] is None):
        rising = ['No data']
    else:
        rising = trends[keyword]['rising'].values.tolist()
    return render_template("gtrend.html",code=code,all_edinetcodeinfo=all_edinetcodeinfo, gtrend_g=dfi,gtrend_t=top,gtrend_r=rising)
예제 #7
0
파일: middlewares.py 프로젝트: fuzzy69/bns
class GtrendsDownloaderMiddleware:
    """
    """
    def __init__(self, proxies=None):
        self._proxies = proxies
        if self._proxies:
            proxie = choice(self._proxies)
            pytrends = TrendReq(hl='en-US', tz=360, proxies = {"https": proxie})
        else:
            self._trends = TrendReq(hl='en-US', tz=360)

    @classmethod
    def from_crawler(cls, crawler):
        proxies = crawler.settings.get("PROXIES")

        return cls(proxies)

    def process_request(self, request, spider):
        self._trends.build_payload(kw_list=request.keywords)
        related_queries = self._trends.related_queries()

        return GtrendsResponse(related_queries=related_queries)

    def process_response(self, request, response, spider):
        return response
예제 #8
0
async def google_trends(request):
    if request.method == 'GET':
        topic = request.args.get('topic').split(',')
        lang = request.args.get('lang')
        region = request.args.get('reg')
    
    else:
        return json({'response': False, 'message': 'Wrong HTTP method', 'results': []})

    if topic is None or topic is '':
        return json({'response': False, 'message': 'Not enough arguments', 'results': []})

    from pytrends.request import TrendReq

    pytrends = TrendReq(hl='{}-{}'.format(lang, region), tz=360)
    pytrends.build_payload(topic)

    res = pytrends.interest_over_time()
    res.drop('isPartial', axis=1, inplace=True)

    rel_topics = pytrends.related_topics()[topic[0]]
    rel_topics.drop('mid', axis=1, inplace=True)

    rel_queries = pytrends.related_queries()[topic[0]]

    countries = pytrends.interest_by_region(resolution='COUNTRY')
    countries = countries[(countries.T != 0).any()]

    from json import loads

    countries = loads(countries.to_json())[topic[0]]

    return json({'message':'done', 'response': True, 'result': {'interest': res.to_json(), 'related_topics': rel_topics.to_json(), 
    'top_queries': rel_queries['top'].to_json(), 'rising_queries': rel_queries['rising'].to_json(), 'countries': countries}})
예제 #9
0
def queries(l_args, s_ticker):
    parser = argparse.ArgumentParser(
        prog='queries',
        description=
        """Print top related queries with this stock's query. [Source: Google]"""
    )

    parser.add_argument('-n',
                        "--num",
                        action="store",
                        dest="n_num",
                        type=check_positive,
                        default=10,
                        help='number of top related queries to print.')

    (ns_parser, l_unknown_args) = parser.parse_known_args(l_args)

    if l_unknown_args:
        print(
            f"The following args couldn't be interpreted: {l_unknown_args}\n")
        return

    pytrend = TrendReq()
    pytrend.build_payload(kw_list=[s_ticker])
    df_related_queries = pytrend.related_queries()
    df_related_queries = df_related_queries[s_ticker]['top'].head(
        ns_parser.n_num)
    df_related_queries['value'] = df_related_queries['value'].apply(
        lambda x: str(x) + '%')
    print(f"Top {s_ticker}'s related queries")
    print(df_related_queries.to_string(index=False))
    print("")
예제 #10
0
def get_related_queries(keyword, location, category):

    path = ""
    pytrend = TrendReq()
    kw_list = [keyword]
    # print "## get_related_queries  ##"
    print kw_list
    print location
    # category = 184
    print category

    pytrend.build_payload(kw_list, geo=location, cat=category)
    # print "edo1"
    try:
        related_queries_dict = pytrend.related_queries()
        # print "edo2"
        rising_df = related_queries_dict[keyword]['rising']
        # pprint.pprint(rising_df)
        # print "edo3"
        # print 'printing dictionary top'
        top_df = related_queries_dict[keyword]['top']
        related_queries = {
            'rising': rising_df.to_dict(orient='record'),
            'top': top_df.to_dict(orient='record')
        }
    except:
        related_queries = {'rising': "", 'top': ""}

    return related_queries
예제 #11
0
def get_related_queries(keyword, category):

    path = ""
    pytrend = TrendReq()
    kw_list = [keyword]
    pytrend.build_payload(kw_list, cat=category)
    # pytrend.build_payload(kw_list=['obama', 'trump'])

    related_queries_dict = pytrend.related_queries()

    # print 'printing dictionary '
    # pprint.pprint(related_queries_dict)

    # print 'printing dictionary rising'
    rising_df = related_queries_dict[keyword]['rising']
    # pprint.pprint(rising_df)

    # print 'printing dictionary top'
    top_df = related_queries_dict[keyword]['top']
    # pprint.pprint(top_df)

    # print 'interest_over_time'
    related_queries = {
        'rising': rising_df.to_dict(orient='record'),
        'top': top_df.to_dict(orient='record')
    }
    # pprint.pprint(interest_over_time)
    # related_queries_json = json.dumps(related_queries_dict)

    return related_queries
예제 #12
0
        def get_data(Start, End):

            Trending_Terms = TrendReq(hl='en-US', tz=360)

            Keywords = ['share price', 'stock price', 'price']

            Trending_Terms.build_payload(kw_list=Keywords,
                                         cat=0,
                                         timeframe=f'{Start} {End}',
                                         geo='US',
                                         gprop='')

            Interest_Over_Time = Trending_Terms.interest_over_time()
            Related_Queries = Trending_Terms.related_queries()

            top_queries = []
            rising_queries = []
            for key, value in Related_Queries.items():
                for k1, v1 in value.items():
                    if (k1 == "top"):
                        top_queries.append(v1)
                    elif (k1 == "rising"):
                        rising_queries.append(v1)
            top_searched = pd.DataFrame(top_queries[1])
            rising_searched = pd.DataFrame(rising_queries[1])

            return top_searched, rising_searched
예제 #13
0
def rise(l_args, s_ticker):
    parser = argparse.ArgumentParser(
        add_help=False,
        prog="rise",
        description="""Print top rising related queries with this stock's query.
                                    [Source: Google]""",
    )
    parser.add_argument(
        "-n",
        "--num",
        action="store",
        dest="n_num",
        type=check_positive,
        default=10,
        help="number of top rising related queries to print.",
    )

    try:
        ns_parser = parse_known_args_and_warn(parser, l_args)
        if not ns_parser:
            return

        pytrend = TrendReq()
        pytrend.build_payload(kw_list=[s_ticker])
        df_related_queries = pytrend.related_queries()
        df_related_queries = df_related_queries[s_ticker]["rising"].head(
            ns_parser.n_num
        )
        print(f"Top rising {s_ticker}'s related queries")
        print(df_related_queries.to_string(index=False))
        print("")

    except Exception as e:
        print(e)
        print("")
예제 #14
0
def queries(l_args, s_ticker):
    parser = argparse.ArgumentParser(
        prog="queries",
        description=
        """Print top related queries with this stock's query. [Source: Google]""",
    )

    parser.add_argument(
        "-n",
        "--num",
        action="store",
        dest="n_num",
        type=check_positive,
        default=10,
        help="number of top related queries to print.",
    )

    try:
        ns_parser = parse_known_args_and_warn(parser, l_args)

        pytrend = TrendReq()
        pytrend.build_payload(kw_list=[s_ticker])
        df_related_queries = pytrend.related_queries()
        df_related_queries = df_related_queries[s_ticker]["top"].head(
            ns_parser.n_num)
        df_related_queries["value"] = df_related_queries["value"].apply(
            lambda x: str(x) + "%")
        print(f"Top {s_ticker}'s related queries")
        print(df_related_queries.to_string(index=False))
        print("")

    except Exception as e:
        print(e)
        print("")
def get_response(
    pytrends_session: TrendReq, keyword_list: List[str], cat: int = 0, geo: str = ""
) -> dict:
    """Returns a dictionary with a dataframe for each keyword
    Calls pytrend's related_queries()

    Args:
        pytrends_session (object): TrendReq() session of pytrend
        keyword_list (list): Used as input for query and passed to TrendReq().build_payload()
        cat (int): see https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories
        geo (str): Geolocation like US, UK

    Returns:
        Dictionary: Dict with dataframes with related query results
    """
    assert isinstance(
        keyword_list, list
    ), f"keyword_list should be string. Instead of type {type(keyword_list)}"

    pytrends_session.build_payload(keyword_list, cat=cat, geo=geo)
    response = pytrends_session.related_queries()
    if response is not None:
        logger.info(f"Query succeeded for {*keyword_list ,}")
    else:
        logger.warning(
            f"Query failed for {*keyword_list ,}, obtained df_related_queries is None."
        )

    return response
예제 #16
0
def get_gtrend(keyword, geo='', timeframe='now 7-d'):
    ''' Gets gGoogle trend data using the Pytrends module

    This functions returns a df with the top related queries related to keyword.
    It also loops over these queries and gets the relative search volume over the last week
    for each search term. This is stored in the rising_queries_interest dict. The key over this 
    dict are the search terms and the values are a df with the relative search volume
    '''
    pytrends = TrendReq()
    pytrends.build_payload([keyword],  geo=geo, timeframe=timeframe)
    while True:
      try:
        results = pytrends.related_queries()
        rising_queries = results[keyword]['rising']
      except:
        time.sleep(60)
        continue
      break

    rising_queries_interest = {}
    if not rising_queries is None:
      for i, row in results[keyword]['rising'].iterrows():
        while True: 
          try:
            pytrends.build_payload([row.query],  geo=geo, timeframe=timeframe)
            rising_queries_interest[row.query] = pytrends.interest_over_time()
          except:
            time.sleep(60)
            continue
          break

    return(rising_queries, rising_queries_interest)
예제 #17
0
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)
예제 #18
0
def get_related_queries(sessiontrend=None,
                        term='',
                        geo='GB',
                        timeframe='today 5-y'):
    kw_list = [term]

    if sessiontrend is None:
        pytrends = TrendReq(hl='en-US', tz=0)
        pytrends.build_payload(kw_list,
                               cat=0,
                               timeframe=timeframe,
                               geo=geo,
                               gprop='')
    else:
        pytrends = sessiontrend

    related_queries = pytrends.related_queries()
    #print(related_queries)
    try:
        if 0 < len(related_queries[term]['rising'].index) <= 3:
            related_queries = related_queries[term]['rising'].values.tolist()
        elif len(related_queries[term]['rising'].index) == 0:
            related_queries = []
        else:
            related_queries = related_queries[term]['rising'].values.tolist()
    except:
        related_queries = []

    # return related_queries
    return json.dumps(related_queries)
예제 #19
0
    def retrieve_google_trends(self, search, date_range):

        # Set up the trend fetching object
        pytrends = TrendReq(hl='en-US', tz=360)
        kw_list = [search]

        try:

            # Create the search object
            pytrends.build_payload(kw_list,
                                   cat=0,
                                   timeframe=date_range[0],
                                   geo='',
                                   gprop='news')

            # Retrieve the interest over time
            trends = pytrends.interest_over_time()

            related_queries = pytrends.related_queries()

        except Exception as e:
            print('\nGoogle Search Trend retrieval failed.')
            print(e)
            return

        return trends, related_queries
예제 #20
0
def trending(k):
    
    import pandas as pd
    from pytrends.request import TrendReq
    pytrend = TrendReq()
    pytrend.build_payload(kw_list=[k])
    related_queries = pytrend.related_queries()
    return '\n'.join((list((((related_queries)[k])["top"])["query"].to_frame()["query"])))
def get_info_world(timeframe):
    res_df = []
    all_words = [['ada','altcoin','altcoins','bitcoin','bitconnect'],['bitminter', 'blockchain', 'blockchain%20mining','btc', 'cardano'],['crypto', 'cryptocurrencies','cryptocurrency', 'cryptographic', 'dash'], ['doge', 'dogecoin', 'eos','ethereum', 'hashing'],['hashocean', 'hodl', 'ico', 'iota', 'litecoin'],['localbitcoins', 'ltc', 'ltc', 'mercado', 'mining%20cryptocurrency'], ['montero', 'onecoin', 'satoshi', 'stellar', 'stratis'], ['tron', 'xlm', 'xmr', 'xrp', 'zcash']]
    for k in all_words:
        list_query_top = []
        list_query_top_value = []
        list_query_rising = []
        list_query_rising_value = []
        pytrend = TrendReq()
        pytrend.build_payload(kw_list=k ,timeframe=timeframe)
        related_queries_dict = pytrend.related_queries()
        for f in k:
            s_top = related_queries_dict[f]['top']
            s_rising = related_queries_dict[f]['rising']
            if s_rising is not None:
                df_rising = s_rising.where(s_rising > 500)
                df_rising = df_rising.dropna()
            list_rising = df_rising['query'].tolist()
            list_rising2 = df_rising['value'].tolist()
            result_rising = zip(list_rising, list_rising2)
            if s_top is not None:
                df_top = s_top.where(s_top > 50)
                df_top = df_top.dropna()
            list_query_top = df_top['query'].tolist()
            list_query_top2 = df_top['value'].tolist()
            result_top = zip(list_query_top, list_query_top2)
            print(list(result_top))
            for j in result_top:
                url = j[0].replace(" ", "+")
                url2 = 'https://trends.'+'google.com/trends/explore?q=' + f + '+' + url  + '&date={}'.format(timeframe)
                pytrend.build_payload(kw_list=[j[0]],timeframe='now 1-d')
                interest_by_region_df = pytrend.interest_by_region()
                s = interest_by_region_df[j[0]]
                df = s.where(s > 50)
                df = df.dropna()
                df = df.sort_values(ascending=False)
                interest_by_region = pd.Series(df.index.values.tolist())
                weight = pd.Series(list(df.values))
                res_df.append(pd.DataFrame({'Location': pd.Series('Wordwide'), 'Keyword': pd.Series(f), 'Related Queries': pd.Series(j[0]), 'Percentage': pd.Series(str(j[1] * 10)), 'Interest by Region':interest_by_region, 'Weight': weight, 'Google Search Link': pd.Series('<a href="{}">{}</a>'.format(url2, url2))}))
     
        
            for j in result_rising:
                url = j[0].replace(" ", "+")
                url2 = 'https://trends.'+'google.com/trends/explore?q=' + f + '+' + url + '&date={}'.format(timeframe)
                pytrend.build_payload(kw_list=[j[0]],timeframe=timeframe)
                interest_by_region_df = pytrend.interest_by_region()
                s = interest_by_region_df[j[0]]
                df = s.where(s > 50)
                df = df.dropna()
                df = df.sort_values(ascending=False)
                interest_by_region = pd.Series(df.index.values.tolist())
                weight = pd.Series(list(df.values))
                res_df.append(pd.DataFrame({'Location': pd.Series('Wordwide'), 'Keyword': pd.Series(f), 'Related Queries': pd.Series(j[0]), 'Percentage': pd.Series('Breakout'), 'Interest by Region':interest_by_region, 'Weight': weight, 'Google Search Link': pd.Series('<a href="{}">{}</a>'.format(url2, url2))}))
     
        
    df = pd.concat(res_df)
    return df
예제 #22
0
 def related_keyword(string):
     try:
         pytrend_session = TrendReq()
         pytrend_session.build_payload(kw_list=[string])
         related_keyword = pytrend_session.related_queries()
     except KeyError as e: # 키워드가 입력되지 않은 경우
         related_keyword[string]['top'] = None
         related_keyword[string]['rising'] = None
         return related_keyword[string]
     return related_keyword[string]
예제 #23
0
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)
예제 #24
0
파일: utils.py 프로젝트: gem763/bmatch
class Gtrend:
    def __init__(self, brand):
        kw_list = [brand]
        self.brand = brand
        self.pytrends = TrendReq()
        self.pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m', geo='', gprop='froogle')

    def trend(self):
        return self.pytrends.interest_over_time().drop(columns=['isPartial'])

    def queries(self):
        return self.pytrends.related_queries()[self.brand]
예제 #25
0
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)
예제 #26
0
def get_related_queries():
    string = request.args.get('keyword')
    try:
        pytrend_session = TrendReq(hl='en-US', tz=120)
        pytrend_session.build_payload(kw_list=[string], cat=30, timeframe='today 12-m', geo='', gprop='')
        related_keyword = pytrend_session.related_queries()
        if related_keyword[string]['top'] is None:
            raise KeyError

    except KeyError as e: # 키워드가 입력되지 않은 경우
        return ''

    return render_template('search/delayed/related_keywords.html', related_keyword=related_keyword[string]['top']['query'])
예제 #27
0
def get_trending_searches():
    list = []
    pytrends = TrendReq(hl='en-US', tz=360)
    pytrends.build_payload(kw_list=['coronavirus'])
    related_queries = pytrends.related_queries()

    for i in related_queries.values():
        df = i['top']
        for index, row in df.iterrows():
            # print(row['query'])
            list.append(row['query'])

    return list
def related_table_data():
    final_mapping = {}
    most_recent_nonfail = {}

    start = datetime.datetime.strptime("19-04-2015", "%d-%m-%Y")
    end = datetime.datetime.strptime("12-04-2020", "%d-%m-%Y")
    dates = list(rrule(
        WEEKLY,
        dtstart=start,
        until=end,
    ))
    pytrends = TrendReq(hl='en-US', tz=360)
    initial_keywords = ['obesity', 'diet', 'exercise', 'disease', 'cancer']

    with open('final_related_data.json') as json_file:
        init_related = json.load(json_file)

        for item in initial_keywords:
            final_mapping[item] = {}
            most_recent_nonfail[item] = list(init_related[item].keys())
    count = 0
    for date in dates:
        try:
            print('Starting: ' + date.strftime("%Y-%m-%d"))

            date_key = date.strftime("%m/%d/%Y")
            next_day = date + timedelta(days=7)
            dates_str = date.strftime("%Y-%m-%d") + " " + next_day.strftime(
                "%Y-%m-%d")
            pytrends.build_payload(kw_list=initial_keywords,
                                   timeframe=dates_str)
            related = pytrends.related_queries()

            for key in related:
                if type(related[key]['top']) == type(None):
                    print("FAILED " + str(key))
                    final_mapping[key][date_key] = most_recent_nonfail[key]
                else:
                    val = related[key]['top'][:5].to_numpy()
                    val = val[:, 0].tolist()
                    final_mapping[key][date_key] = val
                    most_recent_nonfail[key] = val
            print('Complete: ' + date.strftime("%Y-%m-%d"))
        except:
            date_key = date.strftime("%m/%d/%Y")
            print("*" * 40 + 'FAILED AT: ' + date_key + "*" * 40)
            for key in initial_keywords:
                final_mapping[key][date_key] = most_recent_nonfail[key]
            print('Complete: ' + date.strftime("%Y-%m-%d"))

    print('Done')
예제 #29
0
def getRelatedQueries(keywords, interval, country_id):
    """ Returns top queries for given inputs
    Parameters:
        keywords: list of keywords
        interval: interval with format %Y-%m-%d
        country_id: country ID
    """
    pytrend = TrendReq()
    pytrend.build_payload(kw_list=keywords, timeframe=interval, geo=country_id)
    queries = pytrend.related_queries()
    queries2 = pd.DataFrame()
    for key in queries.keys():
        queries2 = pd.concat([queries2, queries[key]['top']])
    return list(queries2['query'].unique())
예제 #30
0
def get_queries(ticker: str) -> pd.DataFrame:
    """Get related queries from google api [Source: google]

    Parameters
    ----------
    ticker : str
        Stock ticker to compare

    Returns
    -------
    pd.DataFrame
        Dataframe of related queries
    """
    pytrend = TrendReq()
    pytrend.build_payload(kw_list=[ticker])
    return pytrend.related_queries()
예제 #31
0

# 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)
예제 #32
0
 def test_related_queries(self):
     pytrend = TrendReq()
     pytrend.build_payload(kw_list=['pizza', 'bagel'])
     self.assertIsNotNone(pytrend.related_queries())