You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Selenium和Python的KendoUI界面数据爬取问题求助

解决方案:爬取laerm-monitoring.de数据

Selenium 实现方案

出现ElementNotInteractableException通常是因为元素未完全加载、被页面元素遮挡,或者该下拉菜单不是标准<select>标签,直接点击选项会失败。以下是修正后的步骤和代码:

核心处理要点

  • 显式等待元素可交互,避免因页面加载慢导致的异常
  • 先触发下拉菜单展开,再选择目标选项

代码示例

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 os

# 设置Chrome下载路径(可选,指定Excel保存位置)
download_dir = os.path.abspath("./downloads")
os.makedirs(download_dir, exist_ok=True)
chrome_options = webdriver.ChromeOptions()
prefs = {"download.default_directory": download_dir}
chrome_options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 15)

try:
    driver.get("https://laerm-monitoring.de/mittelung?mp=14")
    
    # 1. 选择"Averaging"下拉菜单的"24 hours"
    averaging_dropdown = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Averaging')]/following-sibling::div")))
    averaging_dropdown.click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='24 hours']"))).click()
    
    # 2. 选择"Period"下拉菜单的"maximum"
    period_dropdown = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Period')]/following-sibling::div")))
    period_dropdown.click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='maximum']"))).click()
    
    # 3. 点击Export按钮下载Excel
    export_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Export')]")))
    export_btn.click()
    
    # 等待下载完成(可根据实际情况调整等待时间)
    import time
    time.sleep(5)
    
finally:
    driver.quit()

非Selenium 实现方案(推荐)

直接抓取网站的导出API,无需模拟浏览器,效率更高。通过浏览器开发者工具分析,点击Export按钮后会发送一个携带筛选参数的GET请求,直接请求即可下载Excel。

代码示例

import requests

# 导出API的URL,参数对应24 hours和maximum筛选条件
export_url = "https://laerm-monitoring.de/api/export/excel?mp=14&averaging=24h&period=max"

# 发送请求并保存文件
response = requests.get(export_url)
response.raise_for_status()  # 检查请求是否成功

with open("noise_data.xlsx", "wb") as f:
    f.write(response.content)

print("Excel文件已成功下载")

参数说明

  • mp=14:对应监测点ID(即原URL中的mp=14)
  • averaging=24h:对应"Averaging"的"24 hours"选项
  • period=max:对应"Period"的"maximum"选项

内容的提问来源于stack exchange,提问作者sdg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 05:05:19