【Mac】Python の Beautifulsoup4 をインストールする方法

Mac で Python の Beautifulsoup4 をインストールする方法を記載します。

環境

OS: macOS Catalina バージョン 10.15.4
Python: Python 3.8.3

pip3 install beautifulsoup4 でインストール

ターミナルを立ち上げてコマンド「pip3 install beautifulsoup4」と入力し、Enter。

~ % pip3 install beautifulsoup4

すると下記の様なログが書き出されます。

 ~ % pip3 install beautifulsoup4
Collecting beautifulsoup4
  Downloading https://files.pythonhosted.org/packages/66/25/ff030e2437265616a1e9b25ccc864e0371a0bc3adb7c5a404fd661c6f4f6/beautifulsoup4-4.9.1-py3-none-any.whl (115kB)
     |████████████████████████████████| 122kB 635kB/s 
Collecting soupsieve>1.2 (from beautifulsoup4)
  Downloading https://files.pythonhosted.org/packages/6f/8f/457f4a5390eeae1cc3aeab89deb7724c965be841ffca6cfca9197482e470/soupsieve-2.0.1-py3-none-any.whl
Installing collected packages: soupsieve, beautifulsoup4
Successfully installed beautifulsoup4-4.9.1 soupsieve-2.0.1
WARNING: You are using pip version 19.2.3, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
 ~ % 

一瞬でダウンロードとインストールが完了し、入力待ち状態になりました。これで完了です。

【Python】pip install requests でエラー pip3 install requests でインストール成功

とりあえず Mac に Python3 のインストールまでは完了

スクレイピングをやってみたいため、requests モジュールをインストールしたい。

環境

OS: macOS Catalina バージョン 10.15.4
Python: Python 3.8.3

「pip install requests」を入力してみる

なるほど、コマンドラインに「pip install requests」と入力すれば良いわけか。コマンドそのまま「requests をインストール」という事ね。

というわけでそのまま Python のシェルで「pip install requests」と入力して Enter。

 % python3
Python 3.8.3 (v3.8.3:6f8c8320e9, May 13 2020, 16:29:34) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> pip install requests

エラー「SyntaxError: invalid syntax」

すると下記の表示が。

>>> pip install requests
  File "<stdin>", line 1
    pip install requests
        ^
SyntaxError: invalid syntax
>>> 

「無効なシンタックス」としてエラーを返されました。なぜ??

調べてみると単純に、pip コマンドは python のシェルで実行するものでなくて、ターミナルを立ち上げた時のデフォルト(?)のコマンドラインで実行するものだと。

>>> pip install requests
  File "<stdin>", line 1
    pip install requests
        ^
SyntaxError: invalid syntax
>>> quit()

なので quit() と入力して一旦 python のシェルから脱出。

そしてpip install requests を再度入力。コレでOK。と思いきや。

エラー「zsh: command not found: pip」

>>> quit()
~ % pip install requests
zsh: command not found: pip

またもやエラー。「pip」というコマンドが見当たりませんと。

なんでや!!!

ということでまた調べたところ、自分は Macbook に python3 インストールして使っているので「pip」ではなく「pip3」を使う必要があったらしい。

「pip3 install requests」でインストール成功

というわけで改めて「pip3 install requests」と入力

そしたらようやく無事にインストール成功しました。

実際に requests モジュールを使ってみる

という事で再度ターミナルで「python3」と入力し Python を立ち上げ、下記を順に入力。

>>> import requests
>>> r = requests.get('http://yahoo.com/')
>>> r.text

そしてエンター!

>>> import requests
>>> r = requests.get('http://yahoo.com/')
>>> r.text
'<!DOCTYPE html>\n<html id="atomic" lang="en-US" class="atomic   l-out Pos-r https fp fp-v2 fp-default mini-uh-on uh-topbar-on ltr desktop Desktop bkt201">\n<head>\n    <meta http-equiv="X-UA-Compatible" content="IE=edge">\n    \n    <title>Yahoo</title><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="//s.yimg.com"><link rel="preconnect" href="//s.yimg.com">

とりあえずヤフーのソースコードがドバドバッと表示されたので、無事 requests モジュールを使える状態となりました!

【Python】Macbook に Python3 をインストールする方法

Macbook に入っていた Python がすでにサポート対象外となっている Python2 系だったので、Python3 系をインストールしたいと思います。

  1. Python3 インストール用ファイルのダウンロード
  2. Python3 のインストール
  3. 証明書のインストール
  4. 動作確認

1. Python3 インストール用ファイルのダウンロード

Python のダウンロードページへアクセスし、ダウンロードリンクをクリック。

2. Python3 のインストール

ダウンロードしたファイルをクリックするとインストーラが起動します。基本的には「続ける」をクリックしていけば問題ありません。

Python のインストール先を指定します。

「インストール」をクリック

無事インストールが完了しました。

今回インストールされたのは Python 3.8。

3. 証明書のインストール

Python フォルダ内にある Install Certificates.command をダブルクリックし、証明書をインストールします。

ターミナルが起動し証明書のインストールが始まります。

Last login: Sun May 17 19:09:43 on ttys000
~ % /Applications/Python\ 3.8/Install\ Certificates.command ; exit;
 -- pip install --upgrade certifi
Collecting certifi
  Downloading https://files.pythonhosted.org/packages/57/2b/26e37a4b034800c960a00c4e1b3d9ca5d7014e983e6e729e33ea2f36426c/certifi-2020.4.5.1-py2.py3-none-any.whl (157kB)
     |████████████████████████████████| 163kB 635kB/s 
Installing collected packages: certifi
Successfully installed certifi-2020.4.5.1
WARNING: You are using pip version 19.2.3, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
 -- removing any existing file or link
 -- creating symlink to certifi certificate bundle
 -- setting permissions
 -- update complete

[プロセスが完了しました]

4. 動作確認

ターミナルで「python3」と入力すると Python3 を開始できます。「python」と入力すると mac にデフォルトで入っている旧バージョンの python が起動するので注意してください。

~ % python3
Python 3.8.3 (v3.8.3:6f8c8320e9, May 13 2020, 16:29:34) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

試しに「print("hello world")」と入力すると、下記の通り「hello world」と表出されました。

>>> print("hello world")
hello world
>>> 

というわけで、Python3 を使用できる状態となりました!