You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Selenium驱动,使其每分钟请求数不超过30次?

当然可以,以下是几种可靠的实现方式

方式一:固定间隔控制(简单直接)

既然每分钟最多30次请求,平均下来就是每2秒一次请求。可以在每次触发请求(比如跳转页面、点击操作、触发接口调用)后,强制等待2秒,确保频率不超标。

示例代码(Python):

import time
from selenium import webdriver

driver = webdriver.Chrome()

# 模拟请求循环
for _ in range(30):
    # 执行你的请求操作,比如访问目标页面
    driver.get("https://target-site.com/page")
    # 等待2秒,保证每分钟请求数不超过30
    time.sleep(2)

driver.quit()

方式二:时间窗口计数控制(更精准)

如果担心短时间内集中触发请求(比如前10秒发满30次,剩下50秒闲置,仍可能触发风控),可以用时间窗口的方式,记录最近60秒内的请求次数,超过阈值就自动等待。

示例代码(Python):

import time
from collections import deque
from selenium import webdriver

driver = webdriver.Chrome()
request_timestamps = deque(maxlen=30)  # 存储最近30次请求的时间戳

def controlled_request(url):
    now = time.time()
    # 移除60秒之前的过期时间戳
    while request_timestamps and now - request_timestamps[0] > 60:
        request_timestamps.popleft()
    # 如果最近60秒已达30次请求,计算需要等待的时长
    if len(request_timestamps) >= 30:
        wait_time = 60 - (now - request_timestamps[0])
        time.sleep(wait_time)
        # 等待后更新时间,再次清理过期记录
        now = time.time()
        while request_timestamps and now - request_timestamps[0] > 60:
            request_timestamps.popleft()
    # 执行请求
    driver.get(url)
    request_timestamps.append(time.time())

# 调用示例
for _ in range(50):
    controlled_request("https://target-site.com/page")

driver.quit()

注意事项

  • 若需统计页面内的异步请求(比如AJAX接口),可以结合Selenium的Network日志监听,精准计数这类请求的频率,而非仅页面跳转次数。
  • 优先使用Selenium的显式等待(WebDriverWait)替代固定sleep,确保元素加载完成后再执行下一步,避免无效等待占用时间。

内容的提问来源于stack exchange,提问作者Xtiaan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 09:25:32