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

Pyppeteer访问Reddit报Navigation Timeout Exceeded超时如何解决?

问题原因

你遇到的超时是两个问题共同导致的:

  1. Reddit的通知授权弹窗阻塞了页面后续交互逻辑
  2. waitForNavigation 调用时机错误,错过了页面跳转的触发事件

解决方案

  • 启动浏览器时添加禁用通知的启动参数,直接阻止弹窗弹出,同时可以添加常用的反检测参数避免被网站识别为爬虫
  • 调整导航等待逻辑,将触发跳转的操作和导航等待放在异步协程里并行执行,避免错过跳转事件
  • 可选优化:将全页面导航等待替换为搜索结果指定元素的等待,不需要等页面所有资源加载完成,稳定性更高

修改后的完整代码

from pyppeteer import launch
from bs4 import BeautifulSoup
import asyncio


async def main(keyword):
    # 启动时添加禁用通知和反检测参数
    browser = await launch(
        headless=True,
        args=[
            '--disable-notifications', # 禁用通知,直接解决弹窗问题
            '--no-sandbox',
            '--disable-infobars',
            '--start-maximized'
        ]
    )
    page = await browser.newPage()
    # 可选:设置正常浏览器UA,避免被反爬识别
    await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36')
    await page.goto('https://www.reddit.com/r/GameDeals/')

    # 输入搜索关键词
    await page.type('[id=header-search-bar]', keyword)
    await page.screenshot({'path': 'main_page.png'})

    # 并行执行回车操作和导航等待,避免错过跳转事件
    await asyncio.gather(
        page.keyboard.press('Enter'),
        page.waitForNavigation(timeout=80000)
    )

    # 可选:替换上面的导航等待为等待搜索结果元素加载,更稳定
    # await page.keyboard.press('Enter')
    # await page.waitForSelector('[data-testid="search-post"]', timeout=80000)

    await page.screenshot({'path': 'results.png'})
    await browser.close()

asyncio.get_event_loop().run_until_complete(main("steam"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 08:15:04