使用Selenium自动化爬取LinkedIn时遇SSL证书错误求助
你在使用Selenium自动化登录LinkedIn并爬取公司数据时,登录后持续遇到SSL证书验证失败、握手失败等错误,具体错误信息如下:
[26224:25040:0314/123926.899:ERROR:cert_verify_proc_builtin.cc(677)] CertVerifyProcBuiltin for ponf.linkedin.com failed:
Certificate i=0 (CN=ponf.linkedin.com,O=LinkedIn Corporation,L=Sunnyvale,ST=California,C=US) -----
ERROR: Time is after notAfter[31640:27472:0314/123926.904:ERROR:ssl_client_socket_impl.cc(992)] handshake failed; returned -1, SSL error code 1, net_error -201
[31640:27472:0314/124052.228:ERROR:ssl_client_socket_impl.cc(992)] handshake failed; returned -1, SSL error code 1, net_error -107
[29816:30512:0314/124106.578:ERROR:gpu_init.cc(525)] Passthrough is not supported, GL is disabled, ANGLE is
[26224:25040:0314/124107.401:ERROR:cert_issuer_source_aia.cc(34)] Error parsing cert retrieved from AIA (as DER):
ERROR: Couldn't read tbsCertificate as SEQUENCE
ERROR: Failed parsing Certificate
错误核心原因
Time is after notAfter是关键错误,说明系统时间与LinkedIn证书有效期不匹配(要么系统时间偏差过大,要么证书路由过程中出现异常),进而引发SSL握手失败;GPU相关错误属于次要问题,不影响核心功能执行。
解决步骤
1. 修正系统时间
证书验证依赖准确的系统时间,若本地时间与实际时间偏差超过证书有效期范围,会触发该错误。手动同步系统时间到当前准确时间后重试。
2. 添加Chrome参数忽略证书错误
在ChromeOptions中添加强制跳过证书验证的参数,绕过当前的证书校验问题:
options = Options() options.add_experimental_option("detach", True) # 添加证书忽略相关参数 options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') options.add_argument('--allow-insecure-localhost')
3. 同步Chrome浏览器与ChromeDriver版本
旧版本浏览器和驱动可能存在证书兼容性问题,确保两者版本完全匹配。webdriver_manager会自动匹配,但建议手动检查Chrome版本,确认驱动同步更新。
4. 启用无痕模式并禁用缓存
初始化浏览器时启动无痕模式,避免缓存中的旧证书干扰:
options.add_argument('--incognito') # 启动无痕模式 options.add_argument('--disable-cache') # 禁用缓存
5. 替换硬编码等待为显式等待
原代码中time.sleep()的硬编码等待稳定性差,替换为Selenium的显式等待,提升代码可靠性:
# 替换time.sleep(20),等待登录邮箱框加载 WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'session_key'))) # 登录后等待搜索框加载,替换time.sleep(30) WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="global-nav-typeahead"]/input')))
6. 检查网络环境
若使用代理或VPN,可能导致证书路由异常,尝试切换到直接网络连接后重试。
优化后的完整代码
import selenium from selenium.webdriver.common.by import By from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import pandas as pd # 配置Chrome选项 options = Options() options.add_experimental_option("detach", True) # 添加证书忽略及无痕模式参数 options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') options.add_argument('--incognito') driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) url = "https://www.linkedin.com/" driver.get(url) driver.maximize_window() sign_in = ['abc223@gmail.com', 'Test123@'] key = ['FlutterWave','Data Analysts', 'Nigeria', ''] # 等待登录邮箱框加载 WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'session_key'))) driver.find_element(By.ID, 'session_key').send_keys(sign_in[0]) # 等待密码框可见并输入密码 WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'session_password'))) driver.find_element(By.ID, 'session_password').send_keys(sign_in[1]) # 等待登录按钮可点击并点击 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="main-content"]/section[1]/div/form[1]/div[2]/button'))).click() # 等待搜索框加载(预留验证码输入时间) WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="global-nav-typeahead"]/input'))) # 执行公司搜索 search = driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input') search.send_keys(key[0]) search.send_keys(Keys.ENTER)
内容的提问来源于stack exchange,提问作者Dayo Salam

