如何用Selenium Python关闭Aeropostale弹窗广告并加载更多商品
解决Aeropostale网站Selenium爬虫广告弹窗无法关闭的问题
核心问题分析
你遇到的广告弹窗差异(手动访问无、爬虫触发有),本质是网站检测到了Selenium的自动化特征,针对性推送了反爬广告。结合你尝试的关闭逻辑无效的情况,可从模拟真实浏览器、优化弹窗定位点击两个维度解决。
一、先规避爬虫检测(从根源减少广告弹出)
给ChromeOptions添加参数,模拟真实用户浏览器环境,避免被网站识别为自动化脚本:
options = webdriver.ChromeOptions() options.add_experimental_option('detach', True) # 关键反检测参数 options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
这一步大概率能让广告不再弹出,直接跳过弹窗处理步骤。
二、优化广告弹窗关闭逻辑(若仍有弹窗)
如果还是出现广告,替换原有的等待和点击逻辑——用element_to_be_clickable确保元素可交互,同时用JavaScript点击绕过遮挡问题:
try: # 等待关闭按钮可点击(比presence_of_element_located更可靠) close_btn = wait.until(EC.element_to_be_clickable((By.ID, "closeIconContainer"))) # 用JS强制点击,避免元素被遮挡无法触发 driver.execute_script("arguments[0].click();", close_btn) print("广告弹窗已关闭") except Exception as e: print(f"未检测到广告弹窗或关闭失败: {e}")
三、修复"显示更多"按钮的点击逻辑
解决广告问题后,优化加载更多的代码,用显式等待替代隐式等待,确保新商品加载完成后再继续点击:
previous_count = 0 while True: try: load_more_btn = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "load-more-button"))) driver.execute_script("arguments[0].click();", load_more_btn) print("点击了一次显示更多") # 等待新商品加载完成(通过商品数量变化判断,比固定等待更准确) wait.until(lambda d: len(d.find_elements(By.CLASS_NAME, "product-pricing")) > previous_count) previous_count = len(driver.find_elements(By.CLASS_NAME, "product-pricing")) except Exception as e: print("没有更多商品可加载,停止点击") break
完整修改后的代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def gather(): options = webdriver.ChromeOptions() options.add_experimental_option('detach', True) # 模拟真实浏览器,规避爬虫检测 options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") driver = webdriver.Chrome(options=options) wait = WebDriverWait(driver, 15) # 延长等待时长到15秒 driver.get("https://www.aeropostale.com/search/?q=shirt") # 处理广告弹窗 try: close_btn = wait.until(EC.element_to_be_clickable((By.ID, "closeIconContainer"))) driver.execute_script("arguments[0].click();", close_btn) print("广告弹窗已关闭") except Exception as e: print(f"未检测到广告弹窗或关闭失败: {e}") # 点击显示更多加载全部商品 previous_count = 0 while True: try: load_more_btn = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "load-more-button"))) driver.execute_script("arguments[0].click();", load_more_btn) print("点击了一次显示更多") # 等待新商品加载完成 wait.until(lambda d: len(d.find_elements(By.CLASS_NAME, "product-pricing")) > previous_count) previous_count = len(driver.find_elements(By.CLASS_NAME, "product-pricing")) except Exception as e: print("没有更多商品可加载,停止点击") break # 提取商品信息 print("\n商品价格列表:") the_prices = driver.find_elements(By.CLASS_NAME,"product-pricing") for price in the_prices: print(price.get_attribute("innerText")) print("\n商品链接列表:") the_links = driver.find_elements(By.CLASS_NAME,"thumb-link") for link in the_links: print(link.get_attribute("href")) print("\n商品名称列表:") product_name = driver.find_elements(By.CLASS_NAME,"product-name") for product in product_name: print(product.get_attribute("innerText")) driver.quit() # 最后关闭浏览器 gather()
内容的提问来源于stack exchange,提问作者Clg9100
相关产品推荐
相关产品推荐

