我想通過自動化下載一堆棒球統計數據的 CSV 文件,可以在以下位置找到:https: //www.fangraphs.com/leaders.aspx ?pos=all&stats=bat&lg=all&qual=0&type=0&season= 2020年&月份=0&季節1=2020&ind=0&團隊=0&羅斯特=0&年齡=0&過濾=&球員=0&開始日期=2020-01-01&結束日期=2020-12-31。將表格下載為 CSV 的按鈕標記為“導出數據”。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>正如您所知,該按鈕不是重定向到下載頁面(在這種情況下requests可用于下載文件),而是一個進程。代碼: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可以單擊該按鈕,但不會啟動下載。有什么方法可以使用Selenium單擊按鈕并下載文件嗎?或者,是否有一些我沒有想到的替代方法?
添加回答
舉報
0/150
提交
取消