我想通過(guò)自動(dòng)化下載一堆棒球統(tǒng)計(jì)數(shù)據(jù)的 CSV 文件,可以在以下位置找到:https: //www.fangraphs.com/leaders.aspx ?pos=all&stats=bat&lg=all&qual=0&type=0&season= 2020年&月份=0&季節(jié)1=2020&ind=0&團(tuán)隊(duì)=0&羅斯特=0&年齡=0&過(guò)濾=&球員=0&開(kāi)始日期=2020-01-01&結(jié)束日期=2020-12-31。將表格下載為 CSV 的按鈕標(biāo)記為“導(dǎo)出數(shù)據(jù)”。HTML:<div class="br_dby"> <span style="float: left"> <a href="javascript:ShowHide();">Show Filters</a> | <a href="#custom">Custom Reports</a> </span> <a href="javascript:__doPostBack('LeaderBoard1$cmdCSV','')" id="LeaderBoard1_cmdCSV">Export Data</a></div>正如您所知,該按鈕不是重定向到下載頁(yè)面(在這種情況下requests可用于下載文件),而是一個(gè)進(jìn)程。代碼:from selenium import webdriverfrom selenium.webdriver.firefox.options import Optionsbinary = r'C:\Program Files\Mozilla Firefox\firefox.exe'options = Options()options.headless = Trueoptions.binary = binaryoptions.set_preference("browser.download.folderList",2)options.set_preference("browser.download.manager.showWhenStarting", True)options.set_preference("browser.download.dir", r"C:\Users\jlpyt\Downloads")driver = webdriver.Firefox(options=options, executable_path=r"C:\Users\jlpyt\geckodriver-v0.27.0-win32\geckodriver.exe")driver.get('https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=0&type=0&season=2020&month=0&season1=2020&ind=0&team=0&rost=0&age=0&filter=&players=0&startdate=2020-01-01&enddate=2020-12-31')elem = driver.find_element_by_id('LeaderBoard1_cmdCSV')elem.click()使用此代碼,Selenium可以單擊該按鈕,但不會(huì)啟動(dòng)下載。有什么方法可以使用Selenium單擊按鈕并下載文件嗎?或者,是否有一些我沒(méi)有想到的替代方法?
添加回答
舉報(bào)
0/150
提交
取消