Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list index out of range after calling .related_queries() method #628

Open
ivanprjcts opened this issue Aug 21, 2024 · 22 comments
Open

list index out of range after calling .related_queries() method #628

ivanprjcts opened this issue Aug 21, 2024 · 22 comments

Comments

@ivanprjcts
Copy link

Hi :)

It looks like .related_queries() method is broken.
Sample code:

from pytrends.request import TrendReq


keywords = ['real madrid']
pytrends = TrendReq(hl='en-US', geo='US')
pytrends.build_payload(keywords)
related_queries_df = pytrends.related_queries()

Traceback:

Traceback (most recent call last):
  ...
  File ".../pytrends/request.py", line 450, in related_queries
    req_json['default']['rankedList'][0]['rankedKeyword'])
IndexError: list index out of range
@sparklette
Copy link

I have the same problem. My guess is that the API has changed. Is there a workaround?

@tanapa2018
Copy link

Received data: {'default': {'rankedList': []}}
pytrends.request: No rankedList data found
pytrends.request: No second rankedList data found for rising queries

On the evening of the 19th, my request was successful, but from the 20th onwards, I got an error: IndexError: list index out of range

@wxqqh
Copy link

wxqqh commented Aug 22, 2024

I have the same problem.

@hsuan619
Copy link

same problem here. It has been 2 days.
Maybe both of related_queries() related_topics() are broken.

@SpearsonWa83643
Copy link

indeed both related_queries() and related_topics() are not working, Google returns 200 but data returned is {'default': {'rankedList': []}} which causes the index out of range issue.
if api changed why would Google send back 200?
has anyone figured out any patches yet?

@FelipeAlvarezRodrigues
Copy link

I have the same problem.
Only by related queries.

@Toffee1878
Copy link

I have the very same issue with Related topics & Related queries...

Are there any updates? Same issues when testing locally or via hosted web page.

@Yuhsuant1994
Copy link

still facing the same issue... anyone have any workaround?

@linda-tong-dentsu
Copy link

same issue here... any updates?

@wakeisle9933
Copy link

Not only related_queries(), but also related_topics() are the same

@shawsa-artc
Copy link

same here, it's broken

@dbitton
Copy link

dbitton commented Sep 15, 2024

Same problem here, related_queries() endpoint is broken, does anyone support this ?

@hcdc-ap
Copy link

hcdc-ap commented Sep 16, 2024

Same here, it's not working

@st1vms
Copy link

st1vms commented Sep 27, 2024

By inspecting the code I found the reason why it is failing.

This error comes from when retrieving tokens, apparently Google inserted a challenge that gets you flagged as USER_TYPE_SCRAPER whenever you fail it.
Once a token/request is retrieved with this flag, calling the data endpoints will return empty json.

In order to work the server must return tokens with a request having USER_TYPE_LEGIT_USER in it.
So in order to make this library work again, the bot-challenge needs to be solved.

@NickWang322
Copy link

same here, any updates?

@douglastehling
Copy link

Has anyone managed to solve this problem?

@philippe-heitzmann
Copy link

Hi, has anyone managed to solve this for related_queries?

@CHHLeo
Copy link

CHHLeo commented Nov 11, 2024

After several tests, I found that google only allowed logged in account to access related queries and related topics, so there is no way to use any scrapper to get those infos now.

@sdil87
Copy link

sdil87 commented Nov 13, 2024

After several tests, I found that google only allowed logged in account to access related queries and related topics, so there is no way to use any scrapper to get those infos now.

This is not entirely accurate. In trendspy, I think I’ve managed to work around this limitation. You can access related queries and topics without needing a logged-in account. I’ve successfully tested it with at least 600 sequential requests (with 1-second pauses), and it worked without issues. That said, if you send too many requests too quickly, you might still run into a 429 error, so pauses are recommended.

Here’s a quick example of how it works:

!pip install trendspy
from trendspy import Trends
tr = Trends()
related = tr.related_queries('python')
print(related)

@CHHLeo
Copy link

CHHLeo commented Nov 14, 2024

After several tests, I found that google only allowed logged in account to access related queries and related topics, so there is no way to use any scrapper to get those infos now.

This is not entirely accurate. In trendspy, I think I’ve managed to work around this limitation. You can access related queries and topics without needing a logged-in account. I’ve successfully tested it with at least 600 sequential requests (with 1-second pauses), and it worked without issues. That said, if you send too many requests too quickly, you might still run into a 429 error, so pauses are recommended.

Here’s a quick example of how it works:

!pip install trendspy
from trendspy import Trends
tr = Trends()
related = tr.related_queries('python')
print(related)

It's work! Thanks for your help, you did a great job.

@douglastehling
Copy link

pip install trendspy

@sdil87 I'm receiving this error. Do You know what is the problem?

Traceback (most recent call last):
  File "E:\Learning\trendspy\main.py", line 3, in <module>
    related = tr.related_queries('python')
  File "E:\Learning\trendspy\venv\lib\site-packages\trendspy\client.py", line 412, in related_queries
    token, data = self._get_token_data(EMBED_QUERIES_URL, locals(), headers=headers, raise_quota_error=True)
  File "E:\Learning\trendspy\venv\lib\site-packages\trendspy\client.py", line 308, in _get_token_data
    raise TrendsQuotaExceededError()
trendspy.client.TrendsQuotaExceededError: API quota exceeded for related queries/topics. To resolve this, you can try:
1. Use a different referer in request headers:
   tr.related_queries(keyword, headers={'referer': 'https://www.google.com/'})
2. Use a different IP address by configuring a proxy:
   tr.set_proxy('http://proxy:port')
   # or
   tr = Trends(proxy={'http': 'http://proxy:port', 'https': 'https://proxy:port'})
3. Wait before making additional requests

@sdil87
Copy link

sdil87 commented Nov 18, 2024

pip install trendspy

@sdil87 I'm receiving this error. Do You know what is the problem?

Traceback (most recent call last):
  File "E:\Learning\trendspy\main.py", line 3, in <module>
    related = tr.related_queries('python')
  File "E:\Learning\trendspy\venv\lib\site-packages\trendspy\client.py", line 412, in related_queries
    token, data = self._get_token_data(EMBED_QUERIES_URL, locals(), headers=headers, raise_quota_error=True)
  File "E:\Learning\trendspy\venv\lib\site-packages\trendspy\client.py", line 308, in _get_token_data
    raise TrendsQuotaExceededError()
trendspy.client.TrendsQuotaExceededError: API quota exceeded for related queries/topics. To resolve this, you can try:
1. Use a different referer in request headers:
   tr.related_queries(keyword, headers={'referer': 'https://www.google.com/'})
2. Use a different IP address by configuring a proxy:
   tr.set_proxy('http://proxy:port')
   # or
   tr = Trends(proxy={'http': 'http://proxy:port', 'https': 'https://proxy:port'})
3. Wait before making additional requests

This is a HTTP 429 "Too Many Requests" error returned by Google Trends API, indicating you've exceeded their rate limiting thresholds. The error message above provides several approaches you can try to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests