在python中使用Selenium从网站下载文件
我正在尝试从该网站下载csv文件。我修改了答案中的代码。这是我的代码:在python中使用Selenium从网站下载文件,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在尝试从该网站下载csv文件。我修改了答案中的代码。这是我的代码: import selenium from selenium import webdriver # To prevent download dialog profile = selenium.webdriver.FirefoxProfile() profile.set_preference('browser.download.folderList', 2) profile.set_preference('browser.do
import selenium
from selenium import webdriver
# To prevent download dialog
profile = selenium.webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/tmp')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
browser = selenium.webdriver.Firefox(profile)
browser.get("https://charts.bitcoin.com/btc/chart/metcalfe-utxo#5m9v")
browser.find_element_by_id('sidebar-hide-btn').click()
browser.find_element_by_id('data-download').click()
当我运行此命令时,会出现以下错误:
Traceback (most recent call last):
File "/home/alfie/venv/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alfie/Documents/pycharm/crypto_data/main.py", line 10, in <module>
browser = selenium.webdriver.Firefox(profile)
File "/home/alfie/venv/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/home/alfie/venv/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
回溯(最近一次呼叫最后一次):
文件“/home/alfie/venv/lib/python3.8/site packages/selenium/webdriver/common/service.py”,第72行,开头
self.process=subprocess.Popen(cmd,env=self.env,
文件“/usr/lib/python3.8/subprocess.py”,第854行,在__
self.\u execute\u child(参数、可执行文件、预执行文件、关闭文件、,
文件“/usr/lib/python3.8/subprocess.py”,第1702行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“geckodriver”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/home/alfie/Documents/pycharm/crypto_data/main.py”,第10行,在
browser=selenium.webdriver.Firefox(配置文件)
文件“/home/alfie/venv/lib/python3.8/site packages/selenium/webdriver/firefox/webdriver.py”,第164行,在__
self.service.start()
文件“/home/alfie/venv/lib/python3.8/site-packages/selenium/webdriver/common/service.py”,第81行,开头
引发WebDriverException(
selenium.common.exceptions.WebDriverException:消息:“geckodriver”可执行文件需要位于路径中。
我不明白它从哪里得到文件“geckodriver”。有人知道我需要做什么来修复这个问题吗?顺便说一句,pycharm中的virtualenv正在使用python 3.8