如何用Python自动找到下载按钮的链接并下载对应的文件?

问题描述

我有权从以下网站下载一些天气数据

https://www.meteobridel.com/messnetz/index3.php#

我想知道是否有可能自动找到“CSV”按钮后面的下载 URL,然后使用 Python 下载该 csv 文件

我试过了,但没有用:

from selenium import webdriver

browser = webdriver.Safari()
url = 'https://meteobridel.lu/?page_id=5'
browser.get(url)

browser.find_element_by_xpath('//*[@id="CSV"]').click()
browser.close()

谢谢!

解决方法

试试

from selenium import webdriver

browser = webdriver.Safari()
url = 'https://meteobridel.lu/?page_id=5'
browser.get(url)

browser.find_element_by_xpath('//body/div[@id='main']/div[1]/div[1]/div[1]/a[4]').click()
browser.close()
,

检查您提供的页面,我找不到“CSV”-ID。 也许尝试按类获取按钮:

browser.find_element_by_xpath(r"//a[contains(@class,'buttons-csv')]").click()
,

元素在 iframe 内,所以你必须先切换到它,因为 frame 的 id 是唯一的,你可以像这样切换

browser.switch_to.frame("iframe")

browser.find_element_by_xpath('//span[contains(text(),"CSV")]/..').click()