返回资讯中心
外部精选
软件工程
#开发者实时流

pytrends-cloud 发布:用云端轮换代理绕开 Google Trends 429 限流

来源标题:Show HN: Pytrends-cloud, pytrends without the 429 errors

pytrends-cloud 作为 pytrends 的替代库发布,通过云端轮换代理发送请求,避免触发 429 TooManyRequestsError。它保留相同的 TrendReq 类和 pandas DataFrame 输出,只替换请求传输层;原 pytrends 仓库已归档且不再维护。用户需安装该库并配置 Apify API token,即可用原有代码继续获取数据。

Hacker News 最新bibian442026/11/27 · 今天10 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

pytrends 的即插即用替代品——pytrends 是 Python 的 Google Trends 库。相同的 TrendReq 类、相同的方法、相同的 pandas DataFrame,但发往 Google 的请求在云端通过轮换代理执行,因此你的脚本不会再被这个错误中断: pytrends.exceptions.TooManyRequestsError: The request failed: Google returned a response with code 429

原版 pytrends 仓库已归档、不再维护,而 Google 仍在持续对它限流。pytrends-cloud 保留你的代码,只替换传输层。

from pytrends.request import TrendReq

from pytrends_cloud import TrendReq pytrends = TrendReq(hl="en-US", tz=360) # reads your APIFY_TOKEN, see below pytrends.build_payload(["python", "javascript", "rust"], timeframe="today 12-m") pytrends.interest_over_time().tail(3)

真实输出,运行于 2026 年 11 月 14 日: python javascript rust isPartial date 2026-10-25 34 4 15 False 2026-11-01 33 4 16 False 2026-11-08 31 4 15 True

安装 pip install pytrends-cloud

需要 Python 3.9 或更高版本。依赖项:requests 和 pandas,与 pytrends 相同。

创建一个免费的 Apify 账户:https://console.apify.com/sign-up 复制你的 API token:https://console.apify.com/settings/integrations 设置一次即可: export APIFY_TOKEN="apify_api_..." # Windows PowerShell: $env:APIFY_TOKEN = "apify_api_..." 或直接传入:TrendReq(apify_token="apify_api_...")。

每次调用都会在 Apify 上运行 Google Trends Scraper,并按结果数量计入你的 Apify 账户费用: 用 TrendReq(max_charge_usd=0.05) 为任意调用设置费用上限。

pytrends 方法 pytrends-cloud build_payload(kw_list, cat, timeframe, geo, gprop) ✅ 最多 5 个词条,支持命名区间("today 5-y"、"now 7-d"……)或日期("2024-01-01 2024-06-30"、"2024-01-01T00 2024-01-07T23"),支持单个 geo 或一组 geo 以对比同一词条在不同地区的表现 interest_over_time() ✅ 相同的 DataFrame:每个词条一个 int 列、isPartial、date 索引 interest_by_region(resolution, inc_low_vol, inc_geo_code) ✅ COUNTRY、REGION、CITY、DMA、低搜索量地区、geoCode 列 related_queries() ✅ {term: {"top": df, "rising": df}} related_topics() ✅ 相同的形状和列;Google 目前对大多数搜索不返回相关主题,因此你会得到 None(pytrends 在这里会抛出 IndexError) trending_searches(pn) ✅ 来自 Google 的每日热门搜索源;pn 是名称("united_states")或代码("US") today_searches(pn) ✅ 返回今日热门搜索的 Series realtime_trending_searches(pn, count) ✅ 列不同:title、approxTraffic、pubDate、newsItems(Google 已停用 pytrends 所用的实时接口) suggestions、categories、top_charts、multirange_interest_over_time、get_historical_interest ❌ 抛出 NotImplementedError:如需支持请提 issue

与 pytrends 的差异 速度。一次数据调用大约需要 5 到 10 秒,相当于一次云端运行的时间。结果按 TrendReq 实例缓存,因此同一方法调用两次只会运行一次。 被忽略的参数。tz、timeout、proxies、retries、backoff_factor 和 requests_args 都会被接受,因此你的代码无需改动即可运行,但这些参数会被忽略:发往 Google 的请求在云端执行。请改用 run_timeout(秒,最大 300)。 异常。pytrends_cloud.exceptions 提供相同的 ResponseError 和 TooManyRequestsError,另有 AuthenticationError 用于 token 缺失或被拒绝的情况。如果 pytrends 仍已安装,它们会继承 pytrends 自身的异常,因此针对 pytrends.exceptions 编写的 except 子句仍能捕获它们。 地区。在美国以外的国家内,resolution="REGION" 或 "CITY" 会生效;pytrends 在这些地方会静默忽略它。 没有静默的零值。如果一次运行提前结束,你的某个词条没有数据而其他词条有数据,你会得到 ResponseError,而不是一列零。 不使用 Python 相同的数据(每次运行最多 25 个词条,支持定时任务、webhook,以及导出为 CSV、Excel 或 Google Sheets)也可作为 Apify Actor 使用:https://apify.com/bibian/google-trends-scraper

与 Google 无关,也与 pytrends 的作者无关。Google Trends 数据由 Google 聚合和匿名化处理。

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →