想学习一下爬虫,安装好selenium后,打开谷歌失败,请问怎么解决呢。

源码:

1、导入selenium

from selenium import webdriver

2、创建浏览器操作对象

path = 'chromedriver.exe'

browser = webdriver.Chrome(path)

3、访问网站

url = 'https://www.jd.com/'

browser.get(url)

错误:
F:\pycharm_stu\crawler_learning\078_爬虫_selenium_基本使用.py:9: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
browser = webdriver.Chrome(path)
Traceback (most recent call last):
File "D:\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "D:\Python310\lib\subprocess.py", line 966, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "F:\pycharm_stu\crawler_learning\078_爬虫_selenium_基本使用.py", line 9, in
browser = webdriver.Chrome(path)
File "D:\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File "D:\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in init
self.service.start()
File "D:\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

Process finished with exit code 1

远程看看吧

正常来说,根本就不需要path,直接webdriver.Chrome()就行,因为下好对应版本之后的webdriver.exe直接放在Python安装目录就好了(具体操作百度很多)

看报错信息好像是 chromedriver.exe 没有设置环境变量,有如下两种解决办法

  1. path 的值为 chromedriver.exe 的绝对路径
  2. chromedriver.exe 配置环境变量

不需要path 把谷歌浏览器的驱动放到python文件夹中后 from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.jd.com') 就可以了

1.显示的问题为,你电脑中的谷歌驱动没有放到path中所以出现报错
2.解决方法:
你将下载的谷歌驱动放到安装python的Scripts文件夹中,搜索python路径的话cmd使用代码where python,将最后一个斜杠中的内容删掉就可以找到
然后使用from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.jd.com')基本上可以打开就可以正常使用了
如果是使用windows下载的python恕无能为力,我也搞不定
ps:如果你在使用谷歌驱动的时候出现了使用的驱动版本不对的问题,你可以尝试其他版本的谷歌驱动,大概率由于谷歌更新版本后把你谷歌版本更改了,需要使用的驱动可能是你最初安装的版本,所以出现这个问题