最近在看视频,里面的老师用到了selenium去打开浏览器并爬取数据。
我尝试了一下,却报错了。
Traceback (most recent call last):
File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/TaiDiProject/Python爬虫助力疫情数据追踪/Pycharm-code/网络爬虫实战/常规动态网页爬取/Selenium打开浏览器.py", line 7, in <module>
driver = webdriver.Edge()
File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 56, in __init__
self.edge_service.start()
File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\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: 'MicrosoftWebDriver.exe' executable needs to be in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687
查了一下,原来是没有把下载好的msedgedriver.exe放到浏览器目录里,而且代码也要说明具体位置。
首先我用的是Microsoft Edge,需要的Drive到这下载
一般解压后就OK了,把里面的msedgedriver.exe放到python.exe目录下和所在浏览器目录下
比如我的浏览器目录:C:\Program Files (x86)\Microsoft\Edge\Application
接着是代码部分,你要说明msedgedriver.exe所在路径(C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe)
from selenium import webdriver
# msedgedriver.exe下载后放到浏览器的目录下
driver = webdriver.Edge("C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe")
driver.get("https://www.ptpress.com.cn/") # 你要进入的浏览器
这样就成功了!