-
Notifications
You must be signed in to change notification settings - Fork 822
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
Still Getting 429 Errors After 4.9.1 Update #573
Comments
Basically, you need to get the NID cookie. Following solution worked for me:
then plug the NID cookie in the
|
Thanks a lot, it worked for me. |
Thanks that worked for me too! |
The solution provided by @uzairamer works for me too. Thanks a lot @uzairamer ! |
Could we incorporate this into the codebase?
…On Mon, Apr 10, 2023 at 11:05 AM, fackse < ***@***.*** > wrote:
The solution provided by @ uzairamer ( https://github.com/uzairamer ) works
for me too. Thanks a lot @ uzairamer ( https://github.com/uzairamer ) !
—
Reply to this email directly, view it on GitHub (
#573 (comment)
) , or unsubscribe (
https://github.com/notifications/unsubscribe-auth/AAIWU4PDAAEDC5TKMSRJW53XARDXDANCNFSM6AAAAAAWYW2WKM
).
You are receiving this because you are subscribed to this thread. Message
ID: <GeneralMills/pytrends/issues/573/1502129017 @ github. com>
|
I don't understand why this solution works at all, this is basically what the GetGoogleCookie method is already doing, or at least trying to do. If someone who can work around the problem using this solution could post more information we could fix it in the library, we need to know at least:
|
It seems that GetGoogleCookie is not working as intended. I accessed to https://trends.google.com/trends/explore?geo=JP, which is used by GetGoogleCookie, from Google Chrome (Secret Window):
https://trends.google.com/trends?geo=JP is ok, and it redirected to https://trends.google.com/home?geo=JP. |
Hi @whatalnk That request is expected to fail with a 429, pytrends doesn't care if it works or not, it's only meant to generate a valid NID cookie. Even when the request fails the backend returns a NID cookie that we can use to the next request(s). Thanks for the response ,though. |
Currently, I want to retrieve the trends for about 5536 terms. Before the hint with the NID cookie came here, I tried it like this: [...]
pytrends = TrendReq(hl='en-US', tz=360, retries=3)
# Set the number of terms per request (maximum 5)
terms_per_request = 5
dfs = []
# Loop through the chunks of unique names
for name_chunk in tqdm(list(chunk(name_time_ranges_tuples, terms_per_request)), desc="Fetching trends data"):
names, time_ranges = zip(*name_chunk)
try:
# Build the payload with the current name chunk
pytrends.build_payload(names, timeframe="2023-03-06 2023-04-03", geo='US')
result = pytrends.interest_over_time()
result = result.drop('isPartial', axis=1)
dfs.append(result)
except Exception as e:
print(f"Error for {names}: {e}")
time.sleep(60)
[...] With the 5536 terms (chunked) I got 230 times the error 429. I didn't follow the progress "live" to the end. But at the beginning, the error came about every second call. Apparently, the error occurred less frequently later. On another machine I used the following code. In this case the abort condition was not reached once: def process_chunk(chunk, index, retries=3):
for attempt in range(retries):
try:
pytrends.build_payload(chunk, timeframe="2023-03-06 2023-04-03", geo='US')
result = pytrends.interest_over_time()
result = result.drop('isPartial', axis=1)
return result
except Exception as e:
if attempt < retries - 1:
print(f"Error processing chunk {index} (attempt {attempt + 1}): {e}. Retrying...")
else:
print(f"Error processing chunk {index} (final attempt {attempt + 1}): {e}. Giving up.")
return None
session = requests.Session()
session.get('https://trends.google.com')
cookies_map = session.cookies.get_dict()
nid_cookie = cookies_map['NID']
proxy = 'http://<user>:<pw>@isp2.hydraproxy.com:9989'
trends_df = pd.DataFrame()
dfs = []
pytrends = TrendReq(hl='en-US', tz=360, retries=3, proxies=[proxy]*100000,requests_args={'headers': {'Cookie': f'NID={nid_cookie}'}})
with Progress() as progress:
task = progress.add_task("[cyan]Processing chunks...", total=len(chunks))
for index, chunk in enumerate(chunks):
result = process_chunk(chunk, index)
if result is not None:
dfs.append(result)
progress.update(task, advance=1)
trends_df = pd.concat(dfs) |
Hi @fackse, I see you're using The
Please, can you try to execute the first solution using Thanks a lot. |
Thank you for your quick reply. This comment was my misunderstandig. Well, I found funny things. I downloaded source code (pytrends==4.9.1) from PyPI, and checked the contents. In requests.py, |
I also took a look at the results from the last run and did some sampling and compared the results to those on the Google Trends page. It showed that pytrends often reports NaNs where values are actually present. I don't want to post the data here publicly, but I can send you an excerpt if you want. Edit: |
kw_list is interpreded as How about
|
You're absolutely right, my bad! 🤦♂️ |
Hi @whatalnk,
Thanks a lot for checking it and raising it here. I've checked the package in pypi.org (both wheel and sdist) and you're right, the version 4.9.1 doesn't contain the fix from the PR #570. The version 4.9.1 is generated from the commit ed8c400dd9e0b52d878187802ad01c4f7e1b9a71 which original branch doesn't contain the code from #570. Please @emlazzarin can you please make a 4.9.2 release from the current master branch? In the meantime, please @fackse install pytrends from the current master branch and retry your code again, to do it you can:
Thank you. |
It appears to be working! I installed it using " pytrends = TrendReq(hl='en-US', tz=360, retries=3, proxies=[proxy]*100000) During the test, I encountered 196 instances of the message "Proxy error. Changing IP". To better control the behavior after the third attempt, I set the retries parameter to 3. Only 15 times did the request fail to go through within three attempts, in which case I had to re-initialize TrendReq (again with 3 retries - as seen in the code above). |
@emlazzarin @Terseus sorry to pester, but any idea when we would get this much needed release? |
Will fix shortly.
…On Thu, Apr 13 2023 at 01:56, datacubed < ***@***.*** > wrote:
@emlazzarin ( https://github.com/emlazzarin ) @Terseus (
https://github.com/Terseus ) sorry to pester, but any idea when we would
get this much needed release?
—
Reply to this email directly, view it on GitHub (
#573 (comment)
) , or unsubscribe (
https://github.com/notifications/unsubscribe-auth/AAIWU4MFC2QRFHAS6RIBZVTXA65VHANCNFSM6AAAAAAWYW2WKM
).
You are receiving this because you were mentioned. Message ID: <GeneralMills/pytrends/issues/573/1506598871
@ github. com>
|
https://pypi.org/project/pytrends/4.9.2/ has now been updated with the correct code. Thanks! |
Hello, I don't know if you will response or not, but it's good to give it a try.
I did research for almost a week, but I don't get any guide from my work place. I would be happy to learn PYTHON. |
|
I'm still getting consistent 429 errors after the latest PyTrends update.
Should I by doing something different? I've read the docs and they look the same as before the PyTrends update.
Thanks.
The text was updated successfully, but these errors were encountered: