什么会导致此函数的范围列表索引遍历 PyTrend 关键字?

问题描述

感谢您看我的问题。

我正在使用伪谷歌趋势 API PyTrends 遍历谷歌趋势数据。当我使用 google 趋势网站时,我可以一起使用多个单词,只要我在单词之间使用“+”符号。例如:“a-kasse+akasse+arbejdsformidling+arbejdsformidlinger+dagpenge+dagpengeperiode”。当我在代码中使用单个单词时,程序会按预期工作。但是当我使用多个单词连接时,它会中断。

当我使用以下函数时,它抛出一个列表索引超出范围错误

def my_funct(Keyword,Dates,Country,Col_name):
 
    KEYWORDS=[Keyword] 

    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= Dates
    COUNTRY=[Country] #Use this link for iso country code
    CATEGORY=0 # Use this link to select categories
    SEARCH_TYPE='' #default is 'web searches',others include 'images','news','youtube','froogle' (google shopping)


    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:
            try:
                pytrend.build_payload(kw_list=keyword,timeframe = DATE_INTERVAL,geo = Country,cat = CATEGORY,gprop = SEARCH_TYPE) 
                dicti[i] = pytrend.interest_over_time()
                i+=1
                time.sleep(9)
                print(dicti)
                
            except requests.exceptions.Timeout:
                    print("Timeout occured")
        
    df_trends = pd.concat(dicti,axis=1)


    df_trends.columns = df_trends.columns.droplevel(0) #drop outside header
    df_trends = df_trends.drop('isPartial',axis = 1) #drop "isPartial"
    df_trends.reset_index(level=0,inplace=True) #reset_index
    df_trends.columns=['date',Col_name] #change column names
  

    return df_trends

我通过另一个函数执行该函数,如下所示:

df_merged3 = excelConcatFunct("a-kasse+akasse+arbejdsformidling+arbejdsformidlinger+dagpenge+dagpengeperiode",'DK','DANISH search terms')

这是该函数的工作原理:

def excelConcatFunct(Word,Country_code,Col_name):

# generic plug-n-chug data
x1 = my_funct(Word,'2004-01-04 2009-01-04',Col_name)
x2 = my_funct(Word,'2009-01-05 2014-01-05',Col_name)
x3 = my_funct(Word,'2014-01-06 2019-01-06',Col_name)              
x4 = my_funct(Word,'2019-01-07 {0}'.format(Today),Col_name)      

# generic plug-n-chug data
df1 = pd.DataFrame(x1)
df2 = pd.DataFrame(x2)
df3 = pd.DataFrame(x3)
df4 = pd.DataFrame(x4)

# Creates an empty dataframe to add to merged df for column spacing
df0 = pd.DataFrame()
df0[''] = ''

# this concats the df horizontally
df_merged = pd.concat([df1,df0,df2,df3,df4],axis=1)
df_merged.reset_index(inplace=True)
# This removes the dangling last column that said "Unamed:0"
df_merged = df_merged.loc[:,~df_merged.columns.str.contains('^Unnamed')]
# This removes the dangling first index column
df_merged = df_merged.loc[:,~df_merged.columns.str.contains('^index')]

# returns the merged dataframe 
return df_merged

这是我收到的错误消息:

  File "C:\Users\JohnReese\Desktop\G_Trends\G_Trender.py",line 111,in <module>
    df_merged3 = excelConcatFunct("a-kasse+akasse+arbejdsformidling+arbejdsformidlinger+dagpenge+dagpengeperiode",'DANISH search terms')

  File "C:\Users\JohnReese\Desktop\G_Trends\G_Trender.py",line 83,in excelConcatFunct
    x1 = my_funct(Word,Col_name)

  File "C:\Users\JohnReese\Desktop\G_Trends\G_Trender.py",line 31,in my_funct
    KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS]

  File "C:\Users\JohnReese\Desktop\G_Trends\G_Trender.py",in <listcomp>
    KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS]

IndexError: list index out of range

请帮忙。欢迎提供任何帮助。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)