Search code examples
python-3.xgoogle-trends

Python error: IndexError: list index out of range (Google trends data)


I have to get google trends data from a list of KEWYWORDS. I'm used to analyze data with STATA and this is my first time that I'm using Python (via project jupiter notebook). I cannot do it via STATA since it doesn't have a package to download google trends data. It seems that Python it does via "pytrends" Here my codes

pip install pytrends
import pandas as pd
import pytrends
from pytrends.request import TrendReq
pytrend = TrendReq()
KEYWORDS['GYMM','GYNE','GYNX']
KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS] 

Here I have this problem "--------------------------------------------------------------------------- IndexError Traceback (most recent call last) in ----> 1 KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS]

in (.0) ----> 1 KEYWORDS_CODES=[pytrend.suggestions(keyword=i)[0] for i in KEYWORDS]

IndexError: list index out of range"

I run the codes one by one and this messagge appears when I type the last line of codes. Just to be honest, I found this code on a web guide for those interested in taking Google trends Data (unfortunately since I don't have a knowledge on Python I'm not able to build my own code). Actually my keywords are copy and paste from txt file (Here I wrote few of them, nevertheless they are thousands)

Would you be so kind to give me some suggestions?

Thanks for your time.


Solution

  • The result of pytrend.suggestions(keyword='GYNX') (the third item in your list) is an empty list [], so indexing it via [0] returns the IndexError