python - 为什么我不能通过 PyTrends 遍历多个搜索词/关键字?列表索引超出范围错误?
问题描述
我试图通过以下 PyTrends 函数代码使用六个关键字:
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
这是执行它的代码:
x1 = my_funct('Nike+Puma+Adidas+New Balance+Gucci+Prada', '2004-01-04 2009-01-04', 'DK', 'Fashion keywords')
当我执行该代码时,我收到此错误:
KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS]
IndexError: list index out of range
它适用于 1 个关键字,但随着我添加更多它会中断。
请,非常感谢任何和所有的帮助。
谢谢
解决方案
推荐阅读
- javascript - 通过检查该对象的另一个属性来获取数组中对象的属性
- random - 如何将随机八位字节插入 Terraform 中的 CIDR 块?
- django - 如何在 Django 3+ 中编写表达式?
- javascript - 为什么删除消息会在 messageUpdate 中触发错误?
- reporting-services - 报告生成器错误“请求失败,响应为空”
- android - 为 IOS 和 Android 反应原生不同的包名称
- netty - grpc异步请求不等待结果
- loops - 给定一个整数列表和一个整数 k,返回 k 是否可以构建为该列表中任何数字的总和或它们的乘积
- influxdb - InfluxQL 到带有重采样和分组的新任务 FLUX
- c++ - 用户定义的容器不适用于 std::ranges