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

如何用Python自动获取下载按钮链接并下载对应文件:气象网站CSV文件下载求助

Fixing Your Automated CSV Download from Meteobridel

Let's break down why your original Selenium code failed and walk through two reliable solutions—one using a fixed Selenium script, and a lighter alternative with requests that skips the browser entirely.

First, What's Wrong with Your Original Code?

Your script has a couple of key issues:

  • Incorrect XPath syntax: You used " (HTML escaped quotes) in your XPath, which Python doesn't recognize. You need to use actual quotes (either single or double, as long as they don't conflict with your string quotes).
  • No wait for page elements: The page might not have finished loading when you try to click the CSV button, leading to a "no such element" error.
  • Mismatched URL: You mentioned the target page is https://www.meteobridel.com/messnetz/index3.php#, but your code uses https://meteobridel.lu/?page_id=5—these might be different pages, so make sure you're targeting the right one.

Solution 1: Fixed Selenium Script

This version adds explicit waits to ensure elements load, fixes the XPath, and uses proper browser setup.

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

# Initialize Safari driver (adjust if using Chrome/Firefox)
driver = webdriver.Safari()
target_url = "https://www.meteobridel.com/messnetz/index3.php#"

try:
    driver.get(target_url)
    
    # Wait up to 10 seconds for the CSV button to become clickable
    csv_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "CSV"))
    )
    csv_button.click()
    
    # Optional: Wait a few seconds for the download to complete before closing
    driver.implicitly_wait(5)
    
finally:
    # Make sure the browser closes even if an error occurs
    driver.quit()

Notes for this script:

  • If the button's ID isn't actually CSV (sometimes page elements change), right-click the CSV button in your browser, select "Inspect", and check the element's actual id or use a different locator (like By.XPATH with the button's text, e.g., //button[text()='CSV']).
  • For Safari, ensure you have the Safari WebDriver extension enabled (built into Safari's developer tools).

Solution 2: Lighter Alternative with requests

Selenium is overkill if you can directly grab the CSV download URL. Here's how to do it without a browser:

  1. First, inspect the CSV button in your browser to find its href attribute (the direct download URL). For example, if the button links to something like https://www.meteobridel.com/messnetz/download_csv.php, you can use requests to download it directly.
import requests

# Replace this with the actual CSV download URL you find via inspection
csv_download_url = "https://www.meteobridel.com/messnetz/download_csv.php"
save_path = "./meteorological_data.csv"

# Send a GET request to download the file
response = requests.get(csv_download_url)
response.raise_for_status()  # Raise an error if the request fails

# Save the content to a local file
with open(save_path, "wb") as f:
    f.write(response.content)

print(f"CSV file saved successfully to {save_path}")

Why this is better:

  • Faster and uses fewer system resources than running a full browser.
  • Avoids issues with browser compatibility or page loading delays.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 08:57:48