Scrapy環境をCentOSで構築する手順

本記事では、何もない状態からCentOSをインストールし、Scrapy環境を構築するところまでを紹介します。

 

さて、前回の記事情報収集/分析ツールとしてスクレイパーを作ってみようと決意したものの、何から始めればいいのか、という状態です。

 

 

まずは情報収集を進めていくと、PythonのフレームワークであるScrapyを利用することで手軽にクロール&スクレイピングが可能と言うことがわかりました。ゼロから作るのも大変なので、物は試しということで、早速、Scrapy環境を構築してみました。

 

 

既存のサーバ上に構築してもよいのですが、本シリーズでは文字通り何もない状態からのスタートして構築していこうと思います。

 

1.開発&実行環境(サーバ)の調達

何もないところからスタートするので、開発にも運用にも、サーバを調達しなければなりません。ということで、まずは開発/実行環境としてTime4VPSで新規のVPSを調達します。

 

簡単10分!海外格安VPSのTime4VPSの契約方法

お試しということで最小限のスペックで良いため月額0.99ユーロ(約130円!)の格安プランで調達しました。(詳細は上記の記事を参照ください。)

 

続いてOSをインストールします。今回は使い慣れたCentOSを選びました。

簡単7分!Time4VPSでOSのインストールを行う方法

 

これで無事、開発&実行環境であるサーバを調達することが出来ました。

 

$ cat /etc/redhat-release

CentOS Linux release 7.3.1611 (Core)

 

 

2.Pythontoとpipのインストール

VPSの準備ができたところで早速Scrapyを導入していきます。ScrapyはPythonのフレームワークであるため、サーバにPythonがインストールされていることが前提条件となります。

※2017/10/27現在、Python2系と3系のどちらでもサポートされています

 

Python2系であればOSインストール時にデフォルトで入っている場合が多く今回も、CentOSにデフォルトで入っているPython2.7を使います。

 

Pythonバージョンの確認

$ python --version

Python 2.7.5

 

pipのインストール

Pythonではpipというパッケージ管理システムが存在します。Scrapyのパッケージもpip経由でインストールするためpipが導入されていることを確認します。

 

$ pip

-bash: pip: command not found

 

案の定、pipは未インストールでしたので、先に進む前にpipをインストールしておきましょう。

 

3.Scrapyのインストール

無事にPythonとpipの確認が取れたら、pipを使ってScrapyをインストールします。

 

#失敗(初回)

$ pip install scrapy

(中略)
copying src/twisted/logger/__init__.py -> build/lib.linux-x86_64-2.7/twisted/logger
copying src/twisted/logger/_io.py -> build/lib.linux-x86_64-2.7/twisted/logger
copying src/twisted/logger/_flatten.py -> build/lib.linux-x86_64-2.7/twisted/logger
copying src/twisted/logger/_filter.py -> build/lib.linux-x86_64-2.7/twisted/logger
copying src/twisted/logger/_legacy.py -> build/lib.linux-x86_64-2.7/twisted/logger
copying src/twisted/logger/_json.py -> build/lib.linux-x86_64-2.7/twisted/logger
copying src/twisted/internet/test/fake_CAs/thing2.pem -> build/lib.linux-x86_64-2.7/twisted/internet/test/fake_CAs
running build_ext
building ‘twisted.test.raiser’ extension
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/src
creating build/temp.linux-x86_64-2.7/src/twisted
creating build/temp.linux-x86_64-2.7/src/twisted/test
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-si
ze=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions
-fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/includ
e/python2.7 -c src/twisted/test/raiser.c -o build/temp.linux-x86_64-2.7/src/twisted/test/raiser.o
unable to execute gcc: No such file or directory
error: command ‘gcc’ failed with exit status 1

—————————————-
Command “/bin/python -u -c “import setuptools, tokenize;__file__=’/tmp/pip-build-R_B7cW/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 /tmp/pip-1rz67o-record/insta
ll-record.txt –single-version-externally-managed –compile” failed with error code 1 in /tmp/pip-build-R_B7cW/Twisted/

gccがインストールされていないので、pipによるインストールが失敗しました。

 

$ gcc

-bash: gcc: command not found

gccをyumでインストールして、再実行していきます。

 

 

#失敗2(再度トライするも失敗)

$ pip install scrapy

creating build/temp.linux-x86_64-2.7/src/twisted/test
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-si
ze=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions
-fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/includ
e/python2.7 -c src/twisted/test/raiser.c -o build/temp.linux-x86_64-2.7/src/twisted/test/raiser.o
src/twisted/test/raiser.c:4:20: fatal error: Python.h: No such file or directory
#include “Python.h”
^
compilation terminated.
error: command ‘gcc’ failed with exit status 1

今度はPython.hが無いと怒られました。。Python.hはpython-devに含まれているので、こちらも追加でインストールしていきます。python-develは、Pythonの開発に必要なヘッダファイルやライブラリファイルなどが含まれている開発用ツールです。

 

#3度めの正直

$ pip install scrapy

Collecting scrapy
Using cached Scrapy-1.4.0-py2.py3-none-any.whl
Collecting PyDispatcher>=2.0.5 (from scrapy)
Collecting Twisted>=13.1.0 (from scrapy)
Using cached Twisted-17.9.0.tar.bz2
Requirement already satisfied: lxml in /usr/lib64/python2.7/site-packages (from scrapy)
Requirement already satisfied: service-identity in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: w3lib>=1.17.0 in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: pyOpenSSL in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: parsel>=1.1 in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: cssselect>=0.9 in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: six>=1.5.2 in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: queuelib in /usr/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied: zope.interface>=3.6.0 in /usr/lib64/python2.7/site-packages (from Twisted>=13.1.0->scrapy)
Requirement already satisfied: constantly>=15.1 in /usr/lib/python2.7/site-packages (from Twisted>=13.1.0->scrapy)
Requirement already satisfied: incremental>=16.10.1 in /usr/lib/python2.7/site-packages (from Twisted>=13.1.0->scrapy)
Requirement already satisfied: Automat>=0.3.0 in /usr/lib/python2.7/site-packages (from Twisted>=13.1.0->scrapy)
Requirement already satisfied: hyperlink>=17.1.1 in /usr/lib/python2.7/site-packages (from Twisted>=13.1.0->scrapy)
Requirement already satisfied: attrs in /usr/lib/python2.7/site-packages (from service-identity->scrapy)
Requirement already satisfied: pyasn1 in /usr/lib/python2.7/site-packages (from service-identity->scrapy)
Requirement already satisfied: pyasn1-modules in /usr/lib/python2.7/site-packages (from service-identity->scrapy)
Requirement already satisfied: cryptography>=1.9 in /usr/lib64/python2.7/site-packages (from pyOpenSSL->scrapy)
Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages (from zope.interface>=3.6.0->Twisted>=13.1.0->scrapy)
Requirement already satisfied: cffi>=1.7; platform_python_implementation != “PyPy” in /usr/lib64/python2.7/site-packages (from cryptography>
=1.9->pyOpenSSL->scrapy)
Requirement already satisfied: enum34; python_version < “3” in /usr/lib/python2.7/site-packages (from cryptography>=1.9->pyOpenSSL->scrapy)
Requirement already satisfied: asn1crypto>=0.21.0 in /usr/lib/python2.7/site-packages (from cryptography>=1.9->pyOpenSSL->scrapy)
Requirement already satisfied: idna>=2.1 in /usr/lib/python2.7/site-packages (from cryptography>=1.9->pyOpenSSL->scrapy)
Requirement already satisfied: ipaddress; python_version < “3” in /usr/lib/python2.7/site-packages (from cryptography>=1.9->pyOpenSSL->scrap
y)
Requirement already satisfied: pycparser in /usr/lib/python2.7/site-packages (from cffi>=1.7; platform_python_implementation != “PyPy”->cryp
tography>=1.9->pyOpenSSL->scrapy)
Building wheels for collected packages: Twisted
Running setup.py bdist_wheel for Twisted … done
Stored in directory: /root/.cache/pip/wheels/91/c7/95/0bb4d45bc4ed91375013e9b5f211ac3ebf4138d8858f84abbc
Successfully built Twisted
Installing collected packages: PyDispatcher, Twisted, scrapy
Successfully installed PyDispatcher-2.0.5 Twisted-17.9.0 scrapy-1.4.0

 

インストール後確認

% scrapy version

Scrapy 1.4.0

 


 

これで、無事にScrapyを使う準備が整いました。次回は実際にScrapyのプロジェクトを作成して、Spider(クロール&スクレイピングプログラム)を作成していきたいと思います。

 

 

問い合わせフォーム

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください