如何用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 useshttps://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 actualidor use a different locator (likeBy.XPATHwith 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:
- First, inspect the CSV button in your browser to find its
hrefattribute (the direct download URL). For example, if the button links to something likehttps://www.meteobridel.com/messnetz/download_csv.php, you can userequeststo 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
相关产品推荐
相关产品推荐

