LoginSignup
3
8

More than 5 years have passed since last update.

Scrapy をWindowsにインストールする

Posted at

Webスクレイピング、クローリングに便利なScrapyですが、Windows環境にScrapyをインストールする場合に、pipでそのままいかないケースがあります。

Twistedはコンパイルを要する

依存パッケージの一つのTwistedがコンパイルを要するため、コンパイラ(VC++等)が入ってないとインストールできません。

$ pip install scrapy
Collecting scrapy
  Using cached https://files.pythonhosted.org/packages/3e/45/414e87ac8209d537c91575538c5307c20217a6943f555e0ee39f6db4bb0f/Scrapy-1.6.0-py2.py3-none-any.whl
(中略)
    running build_ext
    building 'twisted.test.raiser' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
    ----------------------------------------
Command "c:\users\user\appdata\local\programs\python\python37\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\user\\AppData\\Local\\Temp\\pip-install-7h245q4c\\Twisted\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\user\AppData\Local\Temp\pip-record-nq742j85\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\user\AppData\Local\Temp\pip-install-7h245q4c\Twisted\

解決方法は

  • Visual C++をインストールする
  • コンパイル済みパッケージをインストールする

Visual C++をインストールする

Visual Studioのダウンロードサイトに行き
https://visualstudio.microsoft.com/ja/downloads/

Tools for Visual Studio の中にある Build Tools for Visual Studio 2017 をインストールしてください。
ただし、ディスク容量をそこそこ使用します・・・

コンパイル済みパッケージをインストールする

手っ取り早く済ませたい場合には、コンパイル済みパッケージを利用するという方法があります。
※オフィシャルではないので、自己責任でお願いいたします。

https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
から自身の環境にあったパッケージをダウンロード
例)Python 3.7 Windows(64bit)環境の場合は
  Twisted‑18.9.0‑cp37‑cp37m‑win_amd64.whl

ダウンロードが終わったらファイル名を指定してpipでTwistedをインストール

$ pip install Twisted‑18.9.0‑cp37‑cp37m‑win_amd64.whl
(中略)
Installing collected packages: Twisted
Successfully installed Twisted-18.9.0

これでscrapyのインストール準備はOKです。

$ pip install scrapy
(中略)
Installing collected packages: scrapy
Successfully installed scrapy-1.6.0

最後に

今回のTwisted以外にもライセンスの関係でバイナリパッケージの配布が制限されているケースがあります。本来はコンパイル済みパッケージを使用するのが筋ですが、難しいケースなどはコンパイル済みパッケージをそういう前提を理解した上で利用する方法があることを頭に入れておくとちょっと便利ですよ。

参考サイト https://github.com/scrapy/scrapy/issues/2881

3
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8