python爬蟲學習筆記 3.#(番外) (selenium和chromedriver使用中得問題)

python爬蟲學習筆記 3.#(番外) (selenium和chromedriver使用中得問題)

python爬蟲學習筆記 1.1(通用爬蟲和聚焦爬蟲)
python爬蟲學習筆記 1.2 ( HTTP和HTTPS )
python爬蟲學習筆記 1.3 str和bytes的區別
python爬蟲學習筆記 1.4 (Request簡單使用)request安裝
python爬蟲學習筆記 1.5 (Requests深入)
python爬蟲學習筆記 1.6 (HTTP/HTTPS抓包工具-Fiddler)
python爬蟲學習筆記 1.7 (urllib模塊的基本使用)
python爬蟲學習筆記 1.8 (urllib:get請求和post請求)
python爬蟲學習筆記 1.9 (Handler處理器 和 自定義Opener)
python爬蟲學習筆記 2 (非結構化數據和結構化數據提取)
python爬蟲學習筆記 2.1 (正則表達式re模塊)
python爬蟲學習筆記 2.2 (使用正則表達式得爬蟲得簡單案例)
python爬蟲學習筆記 2.3 (XPath與lxml類庫)
python爬蟲學習筆記 2.4 (使用Xpath得案例)
python爬蟲學習筆記 2.5 (json與JsonPath)
python爬蟲學習筆記 2.6 (糗事百科案例)
python爬蟲學習筆記 2.7 (多線程爬蟲案例(初步瞭解))
python爬蟲學習筆記 2.8 (beautifulsoup4)
python爬蟲學習筆記 2.9 (使用bs4得案例)
python爬蟲學習筆記 3 (動態HTML處理和機器圖像識別)
python爬蟲學習筆記 3.1 (動態HTML介紹)
python爬蟲學習筆記 3.2 (Selenium與PhantomJS)
python爬蟲學習筆記 3.#(番外) (selenium和chromedriver使用中得問題)

問題1:selenium安裝,

解決本人使用Pycharm,anoconda,安裝時pip失敗,直接在setting中找到
在這裏插入圖片描述
install就行了

問題2:chromedriver安裝

解決:
chromedriver的版本一定要與Chrome的版本一致,不然就不起作用。

有兩個下載地址:

1、http://chromedriver.storage.googleapis.com/index.html

2、https://npm.taobao.org/mirrors/chromedriver/
當然,你首先需要查看你的Chrome版本,在瀏覽器中輸入chrome://version/
在這裏插入圖片描述
例如我的版本是72.0.3626,所以下載
在這裏插入圖片描述

配置

解壓壓縮包,找到chromedriver.exe複製到chrome的安裝目錄(其實也可以隨便放一個文件夾)。複製chromedriver.exe文件的路徑並加入到電腦的環境變量中去。具體的:
在這裏插入圖片描述
進入環境變量編輯界面,添加到用戶變量即可,雙擊PATH,將你的文件位置(C:\Program Files (x86)\Google\Chrome\Application\)添加到後面。
在這裏插入圖片描述
完成後在cmd下輸入chromedriver驗證是否安裝成功:
在這裏插入圖片描述

測試

未配置環境也可以,例如:

from selenium import webdriver
import time

def main():
    chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'  #chromedriver的文件位置
    b = webdriver.Chrome(executable_path = chrome_driver)
    b.get('https://www.google.com')
    time.sleep(5)
    b.quit()

if __name__ == '__main__':
    main()

已配置環境變量時

from selenium import webdriver
import time

def main():
    b = webdriver.Chrome()
    b.get('https://www.baidu.com')
    time.sleep(5)
    b.quit()

if __name__ == '__main__':
    main()

如果運行時提示
在這裏插入圖片描述
很可能是chromedriver的版本不對(不要問我怎麼知道的)。
參考博文:參考https://www.cnblogs.com/lfri/p/10542797.html

重點再提醒一下,版本一定要對應!!!!!

然而。。還是有錯

from selenium import webdriver

browser = webdriver.Chrome()

browser.quit()

Traceback (most recent call last):

File “D:\Python36\lib\site-packages\selenium\webdriver\common\service.py”, line 76, in start

stdin=PIPE)

File “D:\Python36\lib\subprocess.py”, line 709, in init

restore_signals, start_new_session)

File “D:\Python36\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:/PycharmProjects/chapter11/seleniumExample.py”, line 2, in

browser = webdriver.Chrome()

File “D:\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py”, line 68, in init

self.service.start()

File “D:\Python36\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

根據錯誤提示:Message: ‘chromedriver’ executable needs to be in PATH我們可以找到原因所在:chromedriver需要放在python安裝路徑下。

**

所以要把chromedriver放到python安裝路徑下就可以了,不要放到chrome安裝目錄下,沒有起作用

**

可以從下面下載chromedriver,不同chrome版本,要下載不同chromedriver的版本,要注意對應呀

下面是chromedriver下載的路徑,請放心下載使用

http://chromedriver.storage.googleapis.com/index.html

參考博文:https://blog.csdn.net/seeol/article/details/92829728

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章