在 macOS 中安装 Python Selenium

Aditya Raj 2024年2月15日 Python Python Selenium
  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。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: 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