NYT搜索结果Selenium点击‘Show more’按钮失败问题求助
NYT搜索结果爬虫点击「Show more」按钮失败问题排查与解决建议
问题描述
我正在参与一个研究项目,需要构建一款能处理《纽约时报》(NYT)搜索结果「Show more」加载功能的网络爬虫。目前正在编写驱动代码,实现自动点击「Show more」按钮直至所有结果加载完成,后续将用Beautiful Soup爬取各链接。但遇到问题:Selenium驱动无法点击「Show more」按钮,运行代码时浏览器弹出后随即关闭,输出no more show more button。我已确认每次点击后CSS选择器未发生变化,也尝试过使用XPATH,恳请提供技术建议。
原代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC import time chrome_path = r"/Users/user_name/Desktop/chromedriver_mac64/chromedriver" service = Service(chrome_path) driver = webdriver.Chrome(service=service) driver.get("https://www.nytimes.com/search?dropmab=false&endDate=20230321&query=covid-19&sort=best&startDate=20200101") time.sleep(3) while True: try: show_more = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.css-vsuiox button'))).click() driver.execute_script("arguments[0].click();", show_more) print("Show more button clicked") time.sleep(2) except: print("No more Show more button") break
实际输出
no more show more button
问题分析与修复方案
核心问题点
- 代码逻辑错误:你在定位元素后直接调用
.click(),导致show_more变量接收的是None(click()方法无返回值),后续执行execute_script时会因参数无效触发异常,直接进入except分支。 - 反爬检测拦截:NYT存在基础反爬机制,默认的Selenium配置会被识别为自动化工具,导致页面渲染异常或元素无法正常加载。
- 元素交互前置条件缺失:按钮可能不在浏览器视口中,直接点击会失败。
修正后的代码示例
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC import time chrome_path = r"/Users/user_name/Desktop/chromedriver_mac64/chromedriver" service = Service(chrome_path) # 配置浏览器选项,规避反爬检测 options = webdriver.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(service=service, options=options) driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.get("https://www.nytimes.com/search?dropmab=false&endDate=20230321&query=covid-19&sort=best&startDate=20200101") # 处理Cookie授权弹窗(如果存在) try: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='GDPR-accept']"))).click() except: pass # 无弹窗则跳过 while True: try: # 先定位可点击的按钮元素,再执行点击操作 show_more = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, '.css-vsuiox button')) ) # 滚动到按钮所在位置,确保元素在视口中 driver.execute_script("arguments[0].scrollIntoView(true);", show_more) time.sleep(1) show_more.click() print("Show more button clicked") time.sleep(3) # 延长等待时间,确保新内容完全加载 except Exception as e: print(f"No more Show more button or error occurred: {str(e)}") break # 调试完成后再启用关闭浏览器 # driver.quit()
额外优化建议
- 替换固定sleep:尽量用
WebDriverWait替代time.sleep,根据元素加载状态动态等待,提升脚本稳定性和效率。 - 验证元素定位:打开浏览器开发者工具,在点击按钮前查看元素的真实DOM结构,确认选择器是否精准(比如是否有动态生成的属性)。
- 无头模式适配:如果需要无头运行,添加
options.add_argument("--headless=new"),同时设置窗口大小options.add_argument("window-size=1920x1080"),避免元素因视口问题无法定位。
内容的提问来源于stack exchange,提问作者canurdon
相关产品推荐
相关产品推荐

