在 macOS 中安裝 Python Selenium

Aditya Raj 2023年1月30日
  1. 使用 Python 包安裝程式 (PIP) 在 macOS 中安裝 Python Selenium
  2. 使用原始碼在 macOS 中安裝 Python Selenium
  3. まとめ
在 macOS 中安裝 Python Selenium

Python 中的 Selenium 框架用於自動執行 Web 抓取等任務。本文將討論在 macOS 中安裝 Selenium 的兩種方法。

使用 Python 包安裝程式 (PIP) 在 macOS 中安裝 Python Selenium

Python 或 PIP 的包安裝程式用於在 Python 中安裝、解除安裝和管理包。如果你的機器上安裝了包安裝程式,則可以使用包安裝程式在 macOS 上安裝 Selenium。

要在 Python 版本 2 中安裝 Selenium,可以在命令列終端中執行以下語句:

pip install selenium

你可以執行以下命令在 Python 3.x 版本中安裝 Selenium:

pip3 install selenium

執行上述命令後,Python Selenium 框架將安裝在你的 macOS 上。

使用原始碼在 macOS 中安裝 Python Selenium

你可以使用原始碼手動安裝 Selenium,而不是使用 PIP。為此,我們將使用以下方法。

首先,我們將下載 Selenium 庫的原始碼。為此,我們將使用 curl 命令。

curl 命令在命令列中執行 URL。我們將 Selenium 框架原始碼的下載連結傳遞給 curl 命令。

必須在其中下載原始碼的檔名在 > 運算子之後給出。

curl https://files.pythonhosted.org/packages/ed/9c/9030520bf6ff0b4c98988448a93c04fcbd5b13cd9520074d8ed53569ccfe/selenium-3.141.0.tar.gz > selenium.tar.gz

執行 curl 命令後,原始碼將以 selenium.tar.gz 檔案中的壓縮格式下載到我們的機器上。

下載壓縮的 tar.gz 檔案後,我們將使用 tar 命令提取檔案內容。tar 命令將壓縮檔案的名稱作為輸入引數,並將內容提取到新資料夾中。

tar -xzvf selenium.tar.gz

執行後,tar 命令提取壓縮檔案中包含的所有檔案。你可以在下圖中觀察到這一點:

tar 命令檔案提取

提取檔案內容後,我們將使用 cd 命令導航到包含檔案內容的新建立資料夾。cd 命令將目錄的名稱作為其輸入引數並導航到給定的目錄。

cd selenium-3.141.0

cd 硒安裝

導航到新資料夾後,我們將使用以下命令執行 setup.py 檔案。執行以下命令後,Selenium 將安裝在你的 macOS 上。

sudo python setup.py install

上述命令將在 Python 2.x 版本中安裝 Selenium。你可以使用以下命令將 Selenium 安裝到 Python 3.x 版中。

sudo python3 setup.py install

在這裡,sudo 命令以管理員模式執行該語句。你將需要管理員密碼才能執行此命令。

一旦命令成功執行,Selenium 將安裝在你的系統上。執行如下圖所示:

Python Selenium 安裝

まとめ

在本文中,我們討論了在 macOS 上安裝 Python Selenium 的兩種方法。

如果你的系統中安裝了 Python 包安裝程式 (PIP),則可以使用第一種方法。否則,你可以使用第二種方法在 macOS 上安裝 python selenium。

作者: Aditya Raj
Aditya Raj avatar Aditya Raj avatar

Aditya Raj is a highly skilled technical professional with a background in IT and business, holding an Integrated B.Tech (IT) and MBA (IT) from the Indian Institute of Information Technology Allahabad. With a solid foundation in data analytics, programming languages (C, Java, Python), and software environments, Aditya has excelled in various roles. He has significant experience as a Technical Content Writer for Python on multiple platforms and has interned in data analytics at Apollo Clinics. His projects demonstrate a keen interest in cutting-edge technology and problem-solving, showcasing his proficiency in areas like data mining and software development. Aditya's achievements include securing a top position in a project demonstration competition and gaining certifications in Python, SQL, and digital marketing fundamentals.

GitHub

相關文章 - Python Selenium