2018年 scrapy 安装中文教程

32次阅读

共计 9747 个字符,预计需要花费 25 分钟才能阅读完成。

安装指南

安装 Scrapy 

Scrapy 在 CPython(默认 Python 实现)和 PyPy(从 PyPy 5.9 开始)下运行 Python 2.7 和 Python 3.4 或更高版本。

如果您使用的是 AnacondaMiniconda,您可以从 conda- forge 通道安装该软件包,该软件包含适用于 Linux,Windows 和 OS X 的最新软件包。

要使用安装 Scrapy conda,请运行:

conda install -c conda-forge scrapy

或者,如果您已经熟悉 Python 包的安装,则可以使用以下命令从 PyPI 安装 Scrapy 及其依赖项:

pip install Scrapy

国内用户使用豆瓣源加速下载

pip install Scrapy-i https://pypi.douban.com/simple

请注意,有时这可能需要根据您的操作系统解决某些 Scrapy 依赖项的编译问题,因此请务必查看 特定 于 平台的安装说明

我们强烈建议您在专用的 virtualenv 中安装 Scrapy,以避免与系统软件包冲突。

有关更详细和平台细节的说明,请继续阅读。

有用的东西

Scrapy 是用纯 Python 编写的,取决于一些关键的 Python 包(以及其他):

  • lxml,一个高效的 XML 和 HTML 解析器
  • parsel,一个在 lxml 之上编写的 HTML / XML 数据提取库,
  • w3lib,一个用于处理 URL 和网页编码的多用途帮助程序
  • twisted,一个异步网络框架
  • cryptographypyOpenSSL,用于处理各种网络级安全需求

Scrapy 经过测试的最小版本是:

  • Twisted
  • 14.0lxml 3.4
  • pyOpenSSL 0.14 ​

Scrapy 可能适用于这些软件包的旧版本,但不保证它会继续工作,因为它没有针对它们进行测试。

其中一些软件包本身依赖于非 Python 软件包,可能需要额外的安装步骤,具体取决于您的平台。请查看 下面的平台特定指南

如果与这些依赖关系有任何问题,请参阅各自的安装说明:

使用虚拟环境(推荐)

我们建议在所有平台上的虚拟环境中安装 Scrapy。

Python 包可以全局安装(也称为系统范围),也可以安装在用户空间中。我们不建议安装 scrapy 系统。

相反,我们建议您在所谓的“虚拟环境”(virtualenv)中安装 scrapy。Virtualenvs 允许您不与已安装的 Python 系统包冲突(这可能会破坏您的一些系统工具和脚本),并且仍然通常使用 pip(没有sudo 和喜欢)安装包。

要开始使用虚拟环境,请参阅virtualenv 安装说明。要全局安装它(全局安装它实际上有帮助),它应该是运行的问题:

$ [sudo] pip install virtualenv

查看本 用户指南,了解如何创建 virtualenv。

注意

如果你使用 Linux 或 OS X,virtualenvwrapper是一个创建 virtualenvs 的便利工具。

一旦你创建了 virtualenv,就可以在其中安装 scrapy pip,就像任何其他 Python 包一样。(有关 您可能需要事先安装的非 Python 依赖项,请参阅下面 的特定 平台的指南)。

可以创建 Python virtualenvs 以默认使用 Python 2,或默认使用 Python 3。

  • 如果你想用 Python 3 安装 scrapy,请在 Python 3 virtualenv 中安装 scrapy。
  • 如果你想用 Python 2 安装 scrapy,请在 Python 2 virtualenv 中安装 scrapy。

平台特定安装说明

Windows

虽然可以使用 pip 在 Windows 上安装 Scrapy,但我们建议您安装 AnacondaMiniconda并使用 conda – forge 通道中的软件包,这样可以避免大多数安装问题。

安装 AnacondaMiniconda 后,安装 Scrapy:

conda install -c conda-forge scrapy

Ubuntu 14.04 或以上

Scrapy 目前正在使用最新版本的 lxml,twisted 和 pyOpenSSL 进行测试,并且与最近的 Ubuntu 发行版兼容。但它也应该支持旧版本的 Ubuntu,比如 Ubuntu 14.04,尽管可能存在 TLS 连接问题。

不要 使用python-scrapyUbuntu 提供的软件包,它们通常太旧而且速度慢,无法赶上最新的 Scrapy。

要在 Ubuntu(或基于 Ubuntu)系统上安装 scrapy,您需要安装这些依赖项:

sudo apt-get install python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev
  • python-devzlib1g-devlibxml2-devlibxslt1-dev 所需要的lxml
  • libssl-dev并且 libffi-dev 是必需的cryptography

如果你想在 Python 3 上安装 scrapy,你还需要 Python 3 开发头文件:

sudo apt-get install python3 python3-dev

virtualenv 中,你可以pip 在之后安装 Scrapy:

pip install scrapy

注意

可以使用相同的非 Python 依赖项在 Debian Jessie(8.0)及更高版本中安装 Scrapy。

Mac 

构建 Scrapy 的依赖项需要存在 C 编译器和开发头。在 OS X 上,这通常由 Apple 的 Xcode 开发工具提供。要安装 Xcode 命令行工具,请打开终端窗口并运行:

xcode-select --install

有一个 已知问题 阻止 pip 更新系统包。必须解决这个问题才能成功安装 Scrapy 及其依赖项。以下是一些建议的解决方案

  • (推荐) 不要 使用系统 python,安装一个不与系统其他部分冲突的新版本。以下是使用 自制 软件包管理器的方法:
    • 按照 https://brew.sh/ 中 的说明安装 自制软件
    • 更新您的 PATH 变量以声明在系统软件包之前应该使用自制软件包(如果您使用 zsh 作为默认 shell,则更 .bashrc 改为:).zshrcecho “export PATH=/usr/local/bin:/usr/local/sbin:$PATH” >> ~/.bashrc
    • 重新加载 .bashrc 以确保发生了更改:source ~/.bashrc
    • 安装 python:brew install python
    • 最新版本的 python 已 pip 与它们捆绑在一起,因此您无需单独安装它。如果不是这样,请升级 python:brew update; brew upgrade python
  • (可选)在隔离的 python 环境中安装 Scrapy。此方法是上述 OS X 问题的解决方法,但它是管理依赖关系的总体良好实践,可以补充第一种方法。virtualenv是一个可以用来在 python 中创建虚拟环境的工具。我们建议您阅读 http://docs.python-guide.org/en/latest/dev/virtualenvs/ 等 教程,以便开始使用。

在完成任何这些变通办法之后,您应该能够安装 Scrapy:

pip install Scrapy

PyPy

我们建议使用最新的 PyPy 版本。测试的版本是 5.9.0。对于 PyPy3,仅测试了 Linux 安装。

大多数 scrapy 依赖类似物现在都有 CPython 的二进制轮,但不适用于 PyPy。这意味着将在安装期间构建这些依赖项。在 OS X 上,您可能会遇到构建加密依赖关系的问题,此处 描述 此问题的解决方案,即导出此命令建议的标志(仅在安装 scrapy 时需要)。除了安装构建依赖项之外,在 Linux 上安装没有特殊问题。在 Windows 上使用 PyPy 安装 scrapy 未经过测试。brew install openssl

您可以通过运行 
scrapy bench
检查 scrapy 是否已正确安装。如果此命令出现错误:
TypeError: ... got 2 unexpected keyword arguments
,则表示 setuptools 无法获取一个特定于 PyPy 的依赖项。要解决此问题,请运行。
pip install 'PyPyDispatcher>=2.1.0'

英文版如下:

Scrapy runs on Python 2.7 and Python 3.4 or above under CPython (default Python implementation) and PyPy (starting with PyPy 5.9).

If you’re using Anaconda or Miniconda, you can install the package from the conda-forge channel, which has up-to-date packages for Linux, Windows and OS X.

To install Scrapy using conda, run:

conda install -c conda-forge scrapy

Alternatively, if you’re already familiar with installation of Python packages, you can install Scrapy and its dependencies from PyPI with:

pip install Scrapy

Note that sometimes this may require solving compilation issues for some Scrapy dependencies depending on your operating system, so be sure to check the Platform specific installation notes.

We strongly recommend that you install Scrapy in a dedicated virtualenv, to avoid conflicting with your system packages.

For more detailed and platform specifics instructions, read on.

Things that are good to know

Scrapy is written in pure Python and depends on a few key Python packages (among others):

  • lxml, an efficient XML and HTML parser
  • parsel, an HTML/XML data extraction library written on top of lxml,
  • w3lib, a multi-purpose helper for dealing with URLs and web page encodings
  • twisted, an asynchronous networking framework
  • cryptography and pyOpenSSL, to deal with various network-level security needs

The minimal versions which Scrapy is tested against are:

  • Twisted 14.0
  • lxml 3.4
  • pyOpenSSL 0.14

Scrapy may work with older versions of these packages but it is not guaranteed it will continue working because it’s not being tested against them.

Some of these packages themselves depends on non-Python packages that might require additional installation steps depending on your platform. Please check platform-specific guides below.

In case of any trouble related to these dependencies, please refer to their respective installation instructions:

Using a virtual environment (recommended)

TL;DR: We recommend installing Scrapy inside a virtual environment on all platforms.

Python packages can be installed either globally (a.k.a system wide), or in user-space. We do not recommend installing scrapy system wide.

Instead, we recommend that you install scrapy within a so-called“virtual environment”(virtualenv). Virtualenvs allow you to not conflict with already-installed Python system packages (which could break some of your system tools and scripts), and still install packages normally with pip (without sudo and the likes).

To get started with virtual environments, see virtualenv installation instructions. To install it globally (having it globally installed actually helps here), it should be a matter of running:

$ [sudo] pip install virtualenv

Check this user guide on how to create your virtualenv.

Note

If you use Linux or OS X, virtualenvwrapper is a handy tool to create virtualenvs.

Once you have created a virtualenv, you can install scrapy inside it with pip, just like any other Python package. (See platform-specific guides below for non-Python dependencies that you may need to install beforehand).

Python virtualenvs can be created to use Python 2 by default, or Python 3 by default.

  • If you want to install scrapy with Python 3, install scrapy within a Python 3 virtualenv.
  • And if you want to install scrapy with Python 2, install scrapy within a Python 2 virtualenv.

Platform specific installation notes

Windows

Though it’s possible to install Scrapy on Windows using pip, we recommend you to install Anaconda or Miniconda and use the package from the conda-forge channel, which will avoid most installation issues.

Once you’ve installed Anaconda or Miniconda, install Scrapy with:

conda install -c conda-forge scrapy

Ubuntu 14.04 or above

Scrapy is currently tested with recent-enough versions of lxml, twisted and pyOpenSSL, and is compatible with recent Ubuntu distributions. But it should support older versions of Ubuntu too, like Ubuntu 14.04, albeit with potential issues with TLS connections.

Don’t use the python-scrapy package provided by Ubuntu, they are typically too old and slow to catch up with latest Scrapy.

To install scrapy on Ubuntu (or Ubuntu-based) systems, you need to install these dependencies:

sudo apt-get install python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev
  • python-devzlib1g-devlibxml2-dev and libxslt1-dev are required for lxml
  • libssl-dev and libffi-dev are required for cryptography

If you want to install scrapy on Python 3, you’ll also need Python 3 development headers:

sudo apt-get install python3 python3-dev

Inside a virtualenv, you can install Scrapy with pip after that:

pip install scrapy

Note

The same non-Python dependencies can be used to install Scrapy in Debian Jessie (8.0) and above.

Mac OS X

Building Scrapy’s dependencies requires the presence of a C compiler and development headers. On OS X this is typically provided by Apple’s Xcode development tools. To install the Xcode command line tools open a terminal window and run:

xcode-select --install

There’s a known issue that prevents pip from updating system packages. This has to be addressed to successfully install Scrapy and its dependencies. Here are some proposed solutions:

  • (Recommended) Don’t use system python, install a new, updated version that doesn’t conflict with the rest of your system. Here’s how to do it using the homebrew package manager:
    • Install homebrew following the instructions in https://brew.sh/
    • Update your PATH variable to state that homebrew packages should be used before system packages (Change .bashrc to .zshrc accordantly if you’re using zsh as default shell):echo “export PATH=/usr/local/bin:/usr/local/sbin:$PATH” >> ~/.bashrc
    • Reload .bashrc to ensure the changes have taken place:source ~/.bashrc
    • Install python:brew install python
    • Latest versions of python have pip bundled with them so you won’t need to install it separately. If this is not the case, upgrade python:brew update; brew upgrade python
  • (Optional) Install Scrapy inside an isolated python environment.This method is a workaround for the above OS X issue, but it’s an overall good practice for managing dependencies and can complement the first method.virtualenv is a tool you can use to create virtual environments in python. We recommended reading a tutorial like http://docs.python-guide.org/en/latest/dev/virtualenvs/ to get started.

After any of these workarounds you should be able to install Scrapy:

pip install Scrapy

PyPy

We recommend using the latest PyPy version. The version tested is 5.9.0. For PyPy3, only Linux installation was tested.

Most scrapy dependencides now have binary wheels for CPython, but not for PyPy. This means that these dependecies will be built during installation. On OS X, you are likely to face an issue with building Cryptography dependency, solution to this problem is described here, that is to brew install openssl and then export the flags that this command recommends (only needed when installing scrapy). Installing on Linux has no special issues besides installing build dependencies. Installing scrapy with PyPy on Windows is not tested.

You can check that scrapy is installed correctly by running scrapy bench. If this command gives errors such as TypeError: ... got 2 unexpected keyword arguments, this means that setuptools was unable to pick up one PyPy-specific dependency. To fix this issue, run pip install 'PyPyDispatcher>=2.1.0'

正文完
 0