2024年Python中Pytrends无法使用:429请求错误求助
解决pytrends请求Google Trends返回429 Too Many Requests错误
运行pytrends代码时持续报错:
pytrends.exceptions.TooManyRequestsError: The request failed: Google returned a response with code 429测试代码如下:
from pytrends.request import TrendReq import pandas as pd # import plotly.express as px import time import json # Create pytrends object pytrends = TrendReq(hl='de-DE', tz=360) # 'De' means consider Germany as the search area # Set your keyword and time frame keyword = ['Analytics'] # for example we give 'Data Science' as the search term timeframe = 'today 5-y' # Time frame: last year # Build the payload for the keyword pytrends.build_payload(kw_list=keyword, cat=0, # Category timeframe=timeframe, geo='CR', # Geographic location, in this case 'Deutschland' gprop='') # Google Search Property interest_over_time_df = pytrends.interest_over_time()已尝试运行代码时使用Google Trends、修改request.py的请求方法、设置代理及重试参数,均无效。
有效解决办法
1. 强制增加请求间隔,降低频率
Google对请求频率管控严格,在关键步骤后添加足够等待时间,避免短时间内重复请求:
from pytrends.request import TrendReq import pandas as pd import time pytrends = TrendReq(hl='de-DE', tz=360) keyword = ['Analytics'] timeframe = 'today 5-y' # 构建payload前等待10秒 time.sleep(10) pytrends.build_payload(kw_list=keyword, cat=0, timeframe=timeframe, geo='CR', gprop='') # 获取数据前等待15秒 time.sleep(15) interest_over_time_df = pytrends.interest_over_time()
如果是批量请求关键词,每个关键词请求后都要加10-20秒间隔。
2. 使用动态代理池(而非单个代理)
单个代理易被Google识别限制,建议用多个高匿代理组成的代理池,每次请求随机切换:
import random from pytrends.request import TrendReq # 替换为可用的高匿代理列表 proxy_pool = [ 'https://xxx.xxx.xxx.xxx:xxxx', 'https://yyy.yyy.yyy.yyy:yyyy', 'https://zzz.zzz.zzz.zzz:zzzz' ] selected_proxy = random.choice(proxy_pool) pytrends = TrendReq( hl='de-DE', tz=360, proxies={'https': selected_proxy}, retries=3, backoff_factor=0.5 ) # 后续请求代码不变
注意:必须用支持HTTPS的高匿代理,公开免费代理大多已被Google拉黑。
3. 导入浏览器Cookie到pytrends
利用浏览器已登录的会话Cookie,绕过临时限制:
- 打开浏览器访问Google Trends,确保已登录Google账号
- 按F12打开开发者工具,在「网络」标签下复制任意请求的Cookie值
- 将Cookie传入pytrends:
from pytrends.request import TrendReq # 替换为你从浏览器复制的完整Cookie字符串 google_cookie = 'NID=xxx; SID=xxx; HSID=xxx; ...' pytrends = TrendReq(hl='de-DE', tz=360) # 解析并更新Cookie cookie_dict = {k:v for k,v in [c.split('=',1) for c in google_cookie.split('; ')]} pytrends.cookies.update(cookie_dict) # 后续请求代码不变
4. 拆分时间范围,分批获取数据
把大时间范围拆成多个小时间段,分批请求后合并数据:
from pytrends.request import TrendReq import pandas as pd import time pytrends = TrendReq(hl='de-DE', tz=360) keyword = ['Analytics'] # 拆分5年时间为多个1年区间 timeframes = ['2019-01-01 2020-01-01', '2020-01-01 2021-01-01', '2021-01-01 2022-01-01', '2022-01-01 2023-01-01', '2023-01-01 2024-01-01'] all_data = pd.DataFrame() for tf in timeframes: time.sleep(15) pytrends.build_payload(kw_list=keyword, cat=0, timeframe=tf, geo='CR', gprop='') df = pytrends.interest_over_time() all_data = pd.concat([all_data, df]) # 去重并按时间排序 all_data = all_data.drop_duplicates().sort_index()
注意:Google Trends的热度是时间段内相对值,合并后若需跨时间段对比,需手动调整基准。
5. 升级/降级pytrends版本
pytrends会更新适配Google反爬机制,尝试切换版本:
# 升级到最新版 pip install --upgrade pytrends # 或降级到之前可用的版本(比如v4.9.2) pip install pytrends==4.9.2
内容的提问来源于stack exchange,提问作者danielwadua
相关产品推荐
相关产品推荐

