我正在尝试学习python web抓取,但是我无法使用任何浏览器使用硒。
from selenium import webdriver
browser = webdriver.Firefox()
这就是我所有的代码,我得到这个错误。
Traceback (most recent call last):
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:\codingpractice\python\python challenge.com.py", line 2, in <module>
browser = webdriver.Firefox()
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00A11350>>
Traceback (most recent call last):
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
我已经尝试了将代码路径添加到Internet上可以找到的所有内容
from selenium import webdriver
browser = webdriver.Firefox("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
在我的环境变量中添加PATH。我似乎无法弄清楚...
对于Firefox和Chrome,您现在都需要下载geckodriver / chromedriver。这些驱动程序对于在已安装的浏览器和硒之间进行通信是必需的。因此,您需要:
pip install selenium
)现在,您可以按照此anwser中的说明将geckodriver添加到您的路径中。或者,您可以像下面这样直接在代码中进行设置:
丁丁 driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')
Firefox: driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句