用selenium 时 WebDriverException:Message: 'chromedriver' executable needs to be in PATH问题解决

用爬虫爬取信息时

发生如下错误:

Traceback (most recent call last):
File “D:\Users\dell\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py”, line 76, in start
stdin=PIPE)
File “D:\Users\dell\Anaconda3\lib\subprocess.py”, line 709, in init
restore_signals, start_new_session)
File “D:\Users\dell\Anaconda3\lib\subprocess.py”, line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:/kaikeba/python_data/Data_Analysis_with_Python-master/L4/anjuke/anjuke_selenium.py”, line 11, in
driver = webdriver.Chrome()
File “D:\Users\dell\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py”, line 73, in init
self.service.start()
File “D:\Users\dell\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py”, line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH.
Please see https://sites.google.com/a/chromium.org/chromedriver/home
Process finished with exit code 1

solution:
下载webdriver,要与谷歌浏览器的版本匹配
1、查看版本号
用selenium 时 WebDriverException:Message: 'chromedriver' executable needs to be in PATH问题解决_第1张图片
2、下载webdriver
下载地址:http://chromedriver.storage.googleapis.com/index.html
用selenium 时 WebDriverException:Message: 'chromedriver' executable needs to be in PATH问题解决_第2张图片
3、解压下载的驱动放到指定目录,代码调用时指定该目录即可。
原来的代码:

import time
from selenium import webdriver
import pandas as pd 
driver = webdriver.Chrome()

改成代码:

import time
from selenium import webdriver
import pandas as pd 

chrome_driver = 'E:\\chromedriver_win32\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)

成功抓取信息:
用selenium 时 WebDriverException:Message: 'chromedriver' executable needs to be in PATH问题解决_第3张图片
参考博客:
https://blog.csdn.net/weixin_43746433/article/details/95237254

你可能感兴趣的:(数据分析)