在Selenium ChromeDriver中绕过连接安全检查,解决nftcalendar.io加载卡顿
解决Selenium打开NFT Calendar卡在“检查连接”的问题
以下是几个针对性的解决办法,按优先级尝试:
1. 优化Chrome配置,绕过反爬检测
网站大概率识别到了自动化浏览器,添加参数模拟真实用户环境:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import pandas as pd from bs4 import BeautifulSoup options = webdriver.ChromeOptions() # 禁用自动化特征检测 options.add_argument('--disable-blink-features=AutomationControlled') # 设置真实用户代理(可替换为你当前浏览器的UA) options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36') # 环境优化参数 options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_experimental_option('excludeSwitches', ['enable-logging']) # 启动时直接最大化窗口 options.add_argument('--start-maximized') # Selenium 4+ 直接传入options即可,无需单独处理desired_capabilities driver = webdriver.Chrome('C:/Users/Kaiwalya/Desktop/chromedriver.exe', options=options) driver.get("https://nftcalendar.io/") # 显式等待页面关键元素加载(替换为页面实际存在的元素选择器) try: WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CLASS_NAME, "calendar-item")) ) finally: html = driver.page_source soup = BeautifulSoup(html, 'html.parser') # 指定解析器避免警告 # 后续数据处理逻辑 driver.quit()
2. 确认ChromeDriver与Chrome版本匹配
- 打开Chrome,输入
chrome://version/查看当前浏览器版本 - 下载主版本号完全一致的ChromeDriver(比如Chrome 118.x对应ChromeDriver 118.x)
- 替换代码中的ChromeDriver路径为新版本文件路径
3. 添加页面等待与重试机制
若因网络波动导致加载卡住,可添加重试逻辑:
from selenium.common.exceptions import TimeoutException max_retries = 3 for attempt in range(max_retries): try: driver.get("https://nftcalendar.io/") WebDriverWait(driver, 30).until( EC.presence_of_element_located((By.TAG_NAME, "body")) ) break except TimeoutException: if attempt == max_retries -1: raise driver.refresh()
4. 用隐身模式启动并禁用扩展
部分浏览器扩展可能干扰页面加载,尝试隐身模式启动:
options.add_argument('--incognito') options.add_argument('--disable-extensions')
内容的提问来源于stack exchange,提问作者Kaiwalya Patil
相关产品推荐
相关产品推荐

