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

使用Selenium无法爬取solanabeach.io验证者页面表格的问题求助

爬取Solanabeach验证节点表格失败问题解决

失败原因

  • 站点部署了Cloudflare反爬机制,你使用的旧版无头Chrome模式特征明显,会被直接拦截,返回的页面是安全验证页,没有目标表格内容
  • 你使用的隐式等待只能保证DOM中存在元素,不能保证异步加载的表格数据渲染完成,取页面源码时表格还未生成
  • 代码中使用的driver.find_element_by_xpath是Selenium 3的废弃写法,运行环境如果是Selenium 4会抛出异常,导致后续逻辑无法执行

修复后代码

from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
# 使用新版无头模式,降低反爬识别概率
options.add_argument('--headless=new')
options.add_argument("--enable-javascript")
options.add_argument('--no-sandbox')
# 移除自动化特征标识
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
# 篡改webdriver指纹,绕过基础反爬检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
})

driver.get("https://solanabeach.io/validators")

# 显式等待表格tbody渲染完成,最长等待30秒
try:
    WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.TAG_NAME, "tbody"))
    )
    page_source = driver.page_source
    api = BeautifulSoup(page_source, 'html.parser')
    table = api.find_all('tbody')
    print(table)
except Exception as e:
    print(f"表格加载失败:{str(e)}")
finally:
    driver.quit()

可选优化方案

  • 如果仍被Cloudflare拦截,可临时注释掉无头模式的配置,打开可视化界面手动完成一次安全验证,后续只要cookie未失效就可以正常爬取
  • 控制请求频率,避免短时间内多次访问触发站点限流规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 22:06:03