[Python] pytrends throws list index out of range when retrieving related queries

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the pytrends library to scrape related queries for a list of keywords. The script works for some queries, but for others (like 'Google' or 'Facebook'), it fails with the following warning:

    ⚠️ Error retrieving related queries for 'Google': list index out of range ⚠️ Error retrieving related queries for 'Facebook': list index out of range

    There’s no traceback, and the script continues running. Here's the relevant part of my code: from pytrends.request import TrendReq

    pytrends = TrendReq(hl='en-US', tz=360) keywords = ['Google', 'Facebook']

    for kw in keywords: try: pytrends.build_payload([kw], cat=0, timeframe='now 7-d', geo='', gprop='') related_queries = pytrends.related_queries() rising = related_queries[kw]['rising'] print(f"Top rising related queries for {kw}:\n", rising) except Exception as e: print(f"⚠️ Error retrieving related queries for '{kw}':", e)

    Continue reading...

Compartilhe esta Página