【发布时间】:2021-03-10 10:13:07
【问题描述】:
我正在尝试从该网站下载 csv 文件:https://invasions.si.edu/nbicdb/arrivals?state=AL&submit=Search+database&begin=2000-01-01&end=2020-11-11&type=General+Cargo&bwms=any
为此,我需要点击下载 CSV 文件的 CSV 按钮。但是,我需要对多个链接执行此操作,这就是为什么我想使用 Selenium 来自动执行单击链接的任务。
我当前运行的代码,但实际上并没有将 csv 文件下载到指定文件夹(或任何地方)。
这是我目前拥有的代码:
import selenium
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
from selenium.common.exceptions import TimeoutException
import time
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=folder") # Set the download Path
driver = webdriver.Chrome(options=options)
url = 'https://invasions.si.edu/nbicdb/arrivals?state=AL&submit=Search+database&begin=2000-01-01&end=2020-11-11&type=General+Cargo&bwms=any'
driver.get(url)
python_button = driver.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "csvbutton", " " ))]')
python_button.click()
如果有任何帮助,我将不胜感激!谢谢
【问题讨论】:
-
您好,您可以帮忙点击带有 CSV 字样的 a 标签吗?如果是这样,一个简单的 xpath 将是 //a[text()='CSV'] 或 //a[@class='csvbutton']。
标签: python selenium csv selenium-webdriver web-scraping