使用Selenium访问Blibli站点提示Access Denied的解决方法问询
Selenium访问Blibli被拒绝访问的解决方法
原因说明
站点反爬系统识别到Selenium启动的自动化浏览器特征(例如window.navigator.webdriver标识、ChromeDriver自带的自动化启动标记等),因此返回403拒绝访问。
报错信息如下:
Access Denied
You don't have permission to access "http://www.blibli.com/login?" on this server.
Reference #18.86fa3b17.1634227013.1a33b6
可行解决方案
方案1:手动修改Chrome启动参数隐藏自动化特征
在初始化ChromeDriver时添加反识别参数,修改后的代码如下:
from selenium import webdriver from time import sleep CHROME_DRIVER_PATH = "C:\\Development\\chromedriver_win32\\chromedriver.exe" URL = "https://www.blibli.com/member/order/retail/ongoing" EMAIL = "rickycandra453@gmail.com" BLIBLI_PASSWORD = "你的站点密码" # 请勿将明文密码硬编码在代码中 class Blibli: def __init__(self): option = webdriver.ChromeOptions() # 关键参数:移除自动化控制标识 option.add_argument("--disable-blink-features=AutomationControlled") # 排除自动化控制开关 option.add_experimental_option("excludeSwitches", ["enable-automation"]) # 关闭自动化控制提示条 option.add_experimental_option('useAutomationExtension', False) # 模拟正常用户的UA,可根据自己的浏览器版本修改 option.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") # Windows环境可选添加,禁用沙箱模式减少异常 option.add_argument("--no-sandbox") self.driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH, options=option) # 执行cdp命令覆盖浏览器的webdriver标识 self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) def login(self): self.driver.get(URL) sleep(3) # 添加基础延迟避免触发频率限制 def get_data(self): pass bot = Blibli() bot.login() bot.get_data()
方案2:使用undetected-chromedriver(稳定性更高)
这是专门针对Selenium反检测优化的驱动库,会自动处理所有隐藏特征的配置,无需手动调整参数,使用步骤如下:
- 安装依赖:
pip install undetected-chromedriver - 修改后的代码:
import undetected_chromedriver as uc from time import sleep URL = "https://www.blibli.com/member/order/retail/ongoing" EMAIL = "rickycandra453@gmail.com" BLIBLI_PASSWORD = "你的站点密码" class Blibli: def __init__(self): # 自动适配对应Chrome版本的驱动,无需手动指定chromedriver路径 self.driver = uc.Chrome() def login(self): self.driver.get(URL) sleep(3) def get_data(self): pass bot = Blibli() bot.login() bot.get_data()
额外注意事项
- 操作间隔不要过短,可添加随机延迟模拟正常用户操作,避免触发站点频率限制
- 若多次访问后仍被拦截,可切换代理IP后再尝试
- 不要在代码中硬编码明文密码,可通过环境变量或者独立配置文件读取密码
内容的提问来源于stack exchange,提问作者Stephen
相关产品推荐
相关产品推荐

