安装指南None

安装Scrapy None

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 ,以避免与系统软件包冲突。

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

有用的东西None

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软件包,可能需要额外的安装步骤,具体取决于您的平台。请查看下面的平台特定指南

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

使用虚拟环境(推荐)None

我们建议在所有平台上的虚拟环境中安装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。

平台特定安装说明None

Windows

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

安装AnacondaMiniconda后,安装Scrapy:

conda install -c conda-forge scrapy

Ubuntu 14.04或以上None

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 None

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

xcode-select --install

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

  • (推荐) __不要__使用系统python,安装一个不与系统其他部分冲突的新版本。以下是使用自制软件包管理器的方法:

    • 按照https://brew.sh/中的说明安装自制软件None
    • 更新您的PATH变量以声明在系统软件包之前应该使用自制软件包(如果您使用zsh作为默认shell,则更.bashrc改为:).zshrcNoneecho “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:

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'