Webページをスクレイピングするために、CentOS 7のpython3.6にbeautifulsoupとrequestsをインストールしました。手順をメモしておきます。
私のCentOS 7では、pipコマンドを直接受け付けてくれない(パスが通っていない?)ので、beautifulsoupのインストールは、python3.6で実行します。
まず、pipのバージョンを確認してみます。
$ python -m pip --version pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)
Python3には、標準でpipがインストールされているとのことでしたが、ない場合には、get-pip.pyを使ってインストールしておきます。
さて、rootユーザーから、下記のように入力してbeautifulsoup4をインストールします。
# python3.6 -m pip install beautifulsoup4 Collecting beautifulsoup4 Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB) ・・・ Installing collected packages: beautifulsoup4 Successfully installed beautifulsoup4-4.6.0 #
同様に、requestsもインストールします。
# python3.6 -m pip install requests Collecting requests Downloading requests-2.18.4-py2.py3-none-any.whl (88kB) ・・・ Installing collected packages: idna, chardet, urllib3, certifi, requests Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 #
インストール済みのパッケージを確認してみます。
# python3.6 -m pip freeze beautifulsoup4==4.6.0 certifi==2017.7.27.1 chardet==3.0.4 idna==2.6 requests==2.18.4 urllib3==1.22 #
確かにインストールできました。
ところで、この作業中をpythonソースコードを収めているディレクトリで行っていたところ、以下のようなAttributeErrorが出てしまい、前に進めなくなって途方に暮れていました。
# python3.6 -m pip install requests Traceback (most recent call last): ・・・ ・・・ AttributeError: module 'threading' has no attribute 'Thread' #
調べたところ、Threadプログラムの練習をした時のプログラムファイル”threading.py”というファイルを作っていたのがまずかったようです。このフォルダを抜け出てからコマンドを実行することで、上記のように無事にインストールが完了しました。threading.pyを作った後、その時はてっきり何かの不具合かと思い、CentOS 7の再インストールをしていたのでした(^^A。