LoginSignup
0
1

More than 3 years have passed since last update.

初心者がPythonでウェブスクレイピング(4) - 2 CloudShell上でスクレイピング実行

Last updated at Posted at 2020-09-27

それでは前回作成したCloud Source RepositroiesのリポジトリにスクレイピングのPGMを追加していきます。

Pythonでのウェブスクレイピング学習のロードマップ

(1)ローカルでとりあえず目的のブツのスクレイピングに成功する。
(2)ローカルでスクレイピングした結果をGoogleスプレッドシートに連携する。
(3)ローカルでcron自動実行を行う。
(4)クラウドサーバー上での無料自動実行に挑戦する。(Google Compute Engine)
  (4)-1 クラウドにテスト用PGMを載せて、CloudShell上で正常稼働させる
  (4)-2 スクレイピングPGMをリポジトリに追加し、CloudShell上で正常稼働させる。 ←いまココ
  (4)-3 ComputeEngineのVMインスタンスを作成して、スクレイピングを自動実行させる。
(5)クラウド上で、サーバーレスでの無料自動実行に挑戦する。(たぶんCloud Functions + Cloud Scheduler)

今回の手順

[1]ローカルのリポジトリにスクレイピングPGMを追加
[2]Cloud Source Repositories のマスターにpush
[3]マスターからCloudShell上のクローンにpull
[4]requirements.txtを使った必要モジュールの一括インストール
[5]CloudShell上でのスクレイピングの実行

[1]ローカルのリポジトリにスクレイピングPGMを追加

ローカルのリポジトリにファイルを追加します。

Macのzsh
11:28:14 [~] % cd gce-cron-test
11:28:25 [~/gce-cron-test] % ls -la
total 40
drwxr-xr-x   7 hoge  staff   224  9 26 11:27 .
drwxr-xr-x+ 45 hoge  staff  1440  9 23 16:45 ..
-rw-r--r--@  1 hoge  staff  6148  9 26 11:26 .DS_Store
drwxr-xr-x  13 hoge  staff   416  9 23 16:49 .git
-rw-r--r--   1 hoge  staff   146  9 21 15:29 cron-test.py
-rw-r--r--@  1 hoge  staff  2352  9 16 17:54 my-web-hoge-app-hogehoge.json
-rw-r--r--   1 hoge  staff  2763  9 17 13:22 requests-test2.py

コミットが必要なファイルがあることを確認し、add、commitを行います。

Macのzsh
11:28:28 [~/gce-cron-test] % git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .DS_Store
        my-web-hoge-app-hogehoge.json
        requests-test2.py

nothing added to commit but untracked files present (use "git add" to track)
11:28:34 [~/gce-cron-test] % 
11:28:52 [~/gce-cron-test] % 
11:28:53 [~/gce-cron-test] % git add .
11:28:58 [~/gce-cron-test] % 
11:29:38 [~/gce-cron-test] % 
11:29:38 [~/gce-cron-test] % git commit -m "Add requests-test to Cloud Source Repositories" 
[master 44abc4d] Add requests-test to Cloud Source Repositories
 3 files changed, 73 insertions(+)
 create mode 100644 .DS_Store
 create mode 100644 my-web-hoge-app-hogehoge.json
 create mode 100644 requests-test2.py

[2]Cloud Source Repositries のマスターにpush

masterへのpusuを行います。

Macのzsh
11:30:13 [~/gce-cron-test] % 
11:30:23 [~/gce-cron-test] % 
11:30:23 [~/gce-cron-test] % git push origin master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 3.48 KiB | 891.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://source.developers.google.com/p/my-gce-app/r/gce-cron-test
   938ea70..44abc4d  master -> master
11:31:37 [~/gce-cron-test] % 

[3]マスターからCloudShell上のクローンにpull

CloudShellで前回クローンしたリポジトリにpullします。

cloudshell
cloudshell:09/26/20 02:54:33 ~/gce-cron-test $ git pull origin master

CloudShellのリポジトリに追加されたことを確認します。(後で、漏らしていたrequirements.txtを追加しています。)

cloudshell
cloudshell:09/26/20 02:55:06 ~/gce-cron-test $
cloudshell:09/26/20 02:55:06 ~/gce-cron-test $ ls -la
total 40
drwxr-xr-x  3 hoge hoge 4096 Sep 26 02:52 .
drwxr-xr-x 13 hoge rvm        4096 Sep 23 11:18 ..
-rw-r--r--  1 hoge hoge   80 Sep 23 11:09 cron.log
-rw-r--r--  1 hoge hoge  146 Sep 23 09:03 cron-test.py
-rw-r--r--  1 hoge hoge 6148 Sep 26 02:47 .DS_Store
drwxr-xr-x  8 hoge hoge 4096 Sep 26 02:52 .git
-rw-r--r--  1 hoge hoge 2352 Sep 26 02:47 my-web-scraping-app-hogehoge.json
-rw-r--r--  1 hoge hoge 2763 Sep 26 02:47 requests-test2.py
-rw-r--r--  1 hoge hoge  334 Sep 26 02:52 requirements.txt

[4]requirements.txtを使った必要モジュールの一括インストール

requirements.txtを使って必要モジュールを一括でインストールします。

cloudshell
cloudshell:09/26/20 02:55:10 ~/gce-cron-test $ pip install -r requirements.txt

pipのlistを確認します。
Macローカルで"pip freeze > requirements.txt"で丸ごと必要なモジュールをrequirements.txtに入れてるので、当然ながらちゃんと揃っています。

cloudshell
cloudshell:09/26/20 02:55:41 ~/gce-cron-test $ pip list
Package              Version
-------------------- ---------
appdirs              1.4.4
beautifulsoup4       4.9.1
cachetools           4.1.1
certifi              2020.6.20
chardet              3.0.4
distlib              0.3.1
filelock             3.0.12
google-auth          1.21.0
google-auth-oauthlib 0.4.1
gspread              3.6.0
httplib2             0.18.1
idna                 2.10
oauth2client         4.1.3
oauthlib             3.1.0
pip                  20.1.1
pyasn1               0.4.8
pyasn1-modules       0.2.8
requests             2.24.0
requests-oauthlib    1.3.0
rsa                  4.6
setuptools           47.1.0
six                  1.15.0
soupsieve            2.0.1
urllib3              1.25.10
virtualenv           20.0.31
WARNING: You are using pip version 20.1.1; however, version 20.2.3 is available.
You should consider upgrading via the '/home/hoge/.pyenv/versions/3.8.5/bin/python3.8 -m pip install --upgrade pip' command.

[5]スクレイピングの実行

スクレイピングのPGM"requests-test2.py"を実行してみます。

cloudshell
cloudshell:09/26/20 02:55:49 ~/gce-cron-test $ python requests-test2.py
Traceback (most recent call last):
  File "requests-test2.py", line 40, in <module>
    sheet = get_gspread_book(secret_key, book_name).worksheet(sheet_name)
  File "requests-test2.py", line 20, in get_gspread_book
    credentials = ServiceAccountCredentials.from_json_keyfile_name(secret_key, scope)
  File "/home/hoge/.pyenv/versions/3.8.5/lib/python3.8/site-packages/oauth2client/service_account.py", line 219, in from_json_keyfile_name
    with open(filename, 'r') as file_obj:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/hoge/git-repository/env2/my-web-hoge-app-hogehoge.json'

うっかり、そんなファイルはありません。Macローカルのフルパス指定のままだったので当然です。
ローカルはVScodeですが、こちらはCloudShellのコードエディターで修正します。

cloudshell
cloudshell:09/26/20 02:55:55 ~/gce-cron-test $ pwd
/home/hoge/gce-cron-test
cloudshell:09/26/20 02:56:12 ~/gce-cron-test $ cloudshell open requests-test2.py

"cloudshell open"コマンドでコードエディターが立ち上がるので、jsonのパスを修正します。
スクリーンショット 2020-09-26 12.00.21.png

再実行です。

cloudshell
cloudshell:09/26/20 03:00:32 ~/gce-cron-test $
cloudshell:09/26/20 03:00:33 ~/gce-cron-test $ python requests-test2.py
2020/09/26 03:01:15 スクレイピングを終了しました。
cloudshell:09/26/20 03:01:18 ~/gce-cron-test $

無事にスクレイピングできました。
尚、ソースの全容はこちら。初心者がPythonでウェブスクレイピング(2)
尚、GCP上の時間はデフォルトでUTCですので、東京時間−9時間となります。
スクリーンショット 2020-09-26 19.15.39.png

次回は、Google Compute EngineにVMを作成して、スクレイピングの動作確認、cronでの自動実行に挑みます。

0
1
0

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
0
1