2024年7月6日 星期六

樹莓派安裝 Selenium v4 的版本問題

因打算在高雄家的 Pi 3 上佈署 Selenium 爬蟲, 但檢視之前 (應該是 2018 年) 所安裝的 Selenium 版本發現是 3.141.0 版 :

pi@raspberrypi:~/ $ pip3 show selenium   
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: /home/pi/.local/lib/python3.7/site-packages
Requires: urllib3
Required-by: 

所以我用 -U 參數更新版本 : 

pi@raspberrypi:~/ $ pip3 install selenium -U   
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting selenium
  Using cached https://www.piwheels.org/simple/selenium/selenium-4.16.0-py3-none-any.whl
Requirement already satisfied, skipping upgrade: trio~=0.17 in /home/pi/.local/lib/python3.7/site-packages (from selenium) (0.22.2)
Requirement already satisfied, skipping upgrade: urllib3[socks]<3,>=1.26 in /home/pi/.local/lib/python3.7/site-packages (from selenium) (1.26.16)
Requirement already satisfied, skipping upgrade: trio-websocket~=0.9 in /home/pi/.local/lib/python3.7/site-packages (from selenium) (0.11.1)
Requirement already satisfied, skipping upgrade: certifi>=2021.10.8 in /home/pi/.local/lib/python3.7/site-packages (from selenium) (2024.2.2)
Requirement already satisfied, skipping upgrade: sortedcontainers in /home/pi/.local/lib/python3.7/site-packages (from trio~=0.17->selenium) (2.4.0)
Requirement already satisfied, skipping upgrade: exceptiongroup>=1.0.0rc9; python_version < "3.11" in /home/pi/.local/lib/python3.7/site-packages (from trio~=0.17->selenium) (1.2.1)
Requirement already satisfied, skipping upgrade: idna in /usr/lib/python3/dist-packages (from trio~=0.17->selenium) (2.6)
Requirement already satisfied, skipping upgrade: outcome in /home/pi/.local/lib/python3.7/site-packages (from trio~=0.17->selenium) (1.3.0.post0)
Requirement already satisfied, skipping upgrade: sniffio in /home/pi/.local/lib/python3.7/site-packages (from trio~=0.17->selenium) (1.3.1)
Requirement already satisfied, skipping upgrade: attrs>=20.1.0 in /home/pi/.local/lib/python3.7/site-packages (from trio~=0.17->selenium) (23.2.0)
Requirement already satisfied, skipping upgrade: PySocks!=1.5.7,<2.0,>=1.5.6; extra == "socks" in /home/pi/.local/lib/python3.7/site-packages (from urllib3[socks]<3,>=1.26->selenium) (1.7.1)
Requirement already satisfied, skipping upgrade: wsproto>=0.14 in /home/pi/.local/lib/python3.7/site-packages (from trio-websocket~=0.9->selenium) (1.2.0)
Requirement already satisfied, skipping upgrade: importlib-metadata; python_version < "3.8" in /home/pi/.local/lib/python3.7/site-packages (from attrs>=20.1.0->trio~=0.17->selenium) (6.7.0)
Requirement already satisfied, skipping upgrade: h11<1,>=0.9.0 in /home/pi/.local/lib/python3.7/site-packages (from wsproto>=0.14->trio-websocket~=0.9->selenium) (0.14.0)
Requirement already satisfied, skipping upgrade: typing-extensions>=3.6.4; python_version < "3.8" in /home/pi/.local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->attrs>=20.1.0->trio~=0.17->selenium) (4.7.1)
Requirement already satisfied, skipping upgrade: zipp>=0.5 in /home/pi/.local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->attrs>=20.1.0->trio~=0.17->selenium) (3.15.0)
Installing collected packages: selenium
  Found existing installation: selenium 3.141.0
    Uninstalling selenium-3.141.0:
      Successfully uninstalled selenium-3.141.0
Successfully installed selenium-4.16.0

哇, 是 v4.16.0 版, 比我 LG 筆電 Win11 上的 v4.12.0 還新. 但悲劇來了, 執行昨晚剛改版添加流通狀態訊息的市圖借書預約爬蟲出現錯誤訊息 : 

pi@raspberrypi:~ $ python3 ksml_books_8.py   
Traceback (most recent call last):
  File "ksml_books_8.py", line 1, in <module>
    from selenium import webdriver
  File "/home/pi/.local/lib/python3.7/site-packages/selenium/webdriver/__init__.py", line 20, in <module>
    from .chrome.webdriver import WebDriver as Chrome  # noqa
  File "/home/pi/.local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 18, in <module>
    from selenium.webdriver.chromium.webdriver import ChromiumDriver
  File "/home/pi/.local/lib/python3.7/site-packages/selenium/webdriver/chromium/webdriver.py", line 19, in <module>
    from selenium.webdriver.common.driver_finder import DriverFinder
  File "/home/pi/.local/lib/python3.7/site-packages/selenium/webdriver/common/driver_finder.py", line 22, in <module>
    from selenium.webdriver.common.selenium_manager import SeleniumManager
  File "/home/pi/.local/lib/python3.7/site-packages/selenium/webdriver/common/selenium_manager.py", line 46
    if (path := os.getenv("SE_MANAGER_PATH")) is not None:
             ^
SyntaxError: invalid syntax

谷歌搜尋找到下面這篇 :


回覆文中有人提到 Selenium v4 要求 Python 3.8 以上, 但又有人說 3.7 就可以. 發問者是在 Python 3.7 上使用 Selenium 4.15 出現同樣錯誤, 而我的 LG 筆電上的 Thonny 是 Python 3.10 + Selenium 4.11.2 跑同樣的爬蟲程式正常, 研判可能是 Selenium 版本問題, 所以我又在 Pi 3 上重新安裝 Selenium 並指定 4.11.2 版 :

pi@raspberrypi:~ $ pip3 install selenium==4.11.2   
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting selenium==4.11.2
  Downloading https://files.pythonhosted.org/packages/10/56/8288d1813a68c1e0638515dbb777fce6d87d0d240e683216f956145310e6/selenium-4.11.2-py3-none-any.whl (7.2MB)
Requirement already satisfied: trio~=0.17 in ./.local/lib/python3.7/site-packages (from selenium==4.11.2) (0.22.2)
Requirement already satisfied: urllib3[socks]<3,>=1.26 in ./.local/lib/python3.7/site-packages (from selenium==4.11.2) (1.26.16)
Requirement already satisfied: trio-websocket~=0.9 in ./.local/lib/python3.7/site-packages (from selenium==4.11.2) (0.11.1)
Requirement already satisfied: certifi>=2021.10.8 in ./.local/lib/python3.7/site-packages (from selenium==4.11.2) (2024.2.2)
Requirement already satisfied: attrs>=20.1.0 in ./.local/lib/python3.7/site-packages (from trio~=0.17->selenium==4.11.2) (23.2.0)
Requirement already satisfied: idna in /usr/lib/python3/dist-packages (from trio~=0.17->selenium==4.11.2) (2.6)
Requirement already satisfied: exceptiongroup>=1.0.0rc9; python_version < "3.11" in ./.local/lib/python3.7/site-packages (from trio~=0.17->selenium==4.11.2) (1.2.1)
Requirement already satisfied: sortedcontainers in ./.local/lib/python3.7/site-packages (from trio~=0.17->selenium==4.11.2) (2.4.0)
Requirement already satisfied: outcome in ./.local/lib/python3.7/site-packages (from trio~=0.17->selenium==4.11.2) (1.3.0.post0)
Requirement already satisfied: sniffio in ./.local/lib/python3.7/site-packages (from trio~=0.17->selenium==4.11.2) (1.3.1)
Requirement already satisfied: PySocks!=1.5.7,<2.0,>=1.5.6; extra == "socks" in ./.local/lib/python3.7/site-packages (from urllib3[socks]<3,>=1.26->selenium==4.11.2) (1.7.1)
Requirement already satisfied: wsproto>=0.14 in ./.local/lib/python3.7/site-packages (from trio-websocket~=0.9->selenium==4.11.2) (1.2.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in ./.local/lib/python3.7/site-packages (from attrs>=20.1.0->trio~=0.17->selenium==4.11.2) (6.7.0)
Requirement already satisfied: h11<1,>=0.9.0 in ./.local/lib/python3.7/site-packages (from wsproto>=0.14->trio-websocket~=0.9->selenium==4.11.2) (0.14.0)
Requirement already satisfied: zipp>=0.5 in ./.local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->attrs>=20.1.0->trio~=0.17->selenium==4.11.2) (3.15.0)
Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in ./.local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->attrs>=20.1.0->trio~=0.17->selenium==4.11.2) (4.7.1)
Installing collected packages: selenium
  Found existing installation: selenium 4.16.0
    Uninstalling selenium-4.16.0:
      Successfully uninstalled selenium-4.16.0
Successfully installed selenium-4.11.2   

重新執行市圖爬蟲程式就沒問題了 : 

pi@raspberrypi:~ $ python3 ksml_books_8.py   
Line 訊息發送成功!
Line 訊息發送成功!

可見軟體不是越新版越好, 有時舊版較穩定. 

如果要更新樹莓派的 Python 版本, 下面這篇文章有介紹如何編譯原始碼的指令 :


指令抄錄如下 :

tar xf Python-3.9.0.tar.xz
cd Python-3.9.0
./configure --enable-optimizations
make
sudo make install

印象中執行 make 會很久. 

沒有留言 :