ロリポップでPythonを使ってスクレイピング。モジュールを指定フォルダにインストールして環境構築。

phtyonロゴ

筆者は、レンタルサーバはロリポップを使っているのですが、Pythonでスクレイピングをするための環境構築で、ちょっと手こずりました。
原因がわかってしまえば単純なことだったので、記事として残しておきます。


環境情報


  • ロリポップレンタルサーバ
  • Python 3.4.1

モジュールのインストールコマンド


ロリポップでPythonのモジュールをインストールする方法は以下になります。
TeraTermにSSHで接続し、以下のコマンドを実行します。

※SSHはデフォルトで使用できないので、ロリポップの管理画面で使用できるように設定変更が必要です。


/usr/local/bin/python -m pip install ※モジュール名

Pythonでスクレイピングをおこなうためには「beautifulsoup3」というモジュールが必要です。
しかし、上記のコマンドで「beautifulsoup3」をインストールしようとするとエラーが発生してしまい、インストールすることができません。


$ /usr/local/bin/python -m pip install beautifulsoup3
Downloading/unpacking beautifulsoup3
  Real name of requirement beautifulsoup3 is beautifulsoup4
  Could not find any downloads that satisfy the requirement beautifulsoup3
Cleaning up...
No distributions at all found for beautifulsoup3
Storing debug log for failure in /home/users/0/oops.jp-sakusaku/.pip/pip.log
[oops.jp-sakusaku@users443 winningCollection]$ /usr/local/bin/python -m pip install beautifulsoup
Downloading/unpacking beautifulsoup
  Downloading BeautifulSoup-3.2.2.tar.gz
  Running setup.py (path:/tmp/pip_build_oops.jp-sakusaku/beautifulsoup/setup.py) egg_info for package beautifulsoup
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip_build_oops.jp-sakusaku/beautifulsoup/setup.py", line 3
        "You're trying to run a very old release of Beautiful Soup under Python 3. This will not work."<>"Please use Beautiful Soup 4, available through the pip package 'beautifulsoup4'."
                                                                                                        ^
    SyntaxError: invalid syntax
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/tmp/pip_build_oops.jp-sakusaku/beautifulsoup/setup.py", line 3

    "You're trying to run a very old release of Beautiful Soup under Python 3. This will not work."<>"Please use Beautiful Soup 4, available through the pip package 'beautifulsoup4'."

                                                                                                    ^

SyntaxError: invalid syntax

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_oops.jp-sakusaku/beautifulsoup
Storing debug log for failure in /home/users/0/oops.jp-sakusaku/.pip/pip.log

インストールとパス設定


インストール、および、Python実行時のパス設定にコツがあります。
前提として、以下のフォルダ構成を前提とします。


------
    |
    |---python
    |    |
    |    |---vendor         ・・・  Pythonの個別モジュールインストール先
    |
    |---test
      |
      |---scriping
           |
           |---scriping.sh  ・・・  作成したプログラムを実行するシェル
           |
           |---module       ・・・  作成プログラムの格納フォルダ

まず、インストールしたいモジュールである「beautifulsoup3」は個別モジュールインストール先である「python/vendor」にインストールする必要があります。
詳しい理由はわからないですが、「mysql-connector-python-rf」はインストール先を選択しなくても正常にインストールできたのですが、どうやら「beautifulsoup3」は自身で作成するフォルダにインストールする必要があるようです。


コマンドは以下になります。
「python」フォルダで以下のコマンドを実行します。


/usr/local/bin/python -m pip install BeautifulSoup --target ./vendor

このコマンドを実行することで、vendorフォルダ配下に「beautifulsoup3」のモジュールがインストールされます。


次にパス設定です。
実行するPythonのプログラムに「python/vendor」へのパスを設定する必要があります。
実行するPythonのプログラムは、「test/scriping/module」に格納されています。


#!/usr/local/bin/python3.4
# coding: utf-8
import os
import sys
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../../python/vendor'))
from bs4 import BeautifulSoup 

これで、Pythonでスクレイピングのプログラムを実行する環境構築は完了です。