おりそ.doc

自分のしたことをアウトプットする場所

【備忘録】API使わずに自動投稿がしたい(導入編)

開発環境

目標

目標は 寄り添い型「タイッツー」で自動投稿する こと

しかし、まだAPI提供が始まっていないため、「APIなし」で出来ないか模索しようと思い、今回の環境を作りました。

やったこと

  • Ubuntu22.04 の box 追加
  • マシン起動
  • pythonあるか確認
  • pip インストール
  • selenium インストール
  • google-chrome インストール
  • chrome のドライバーインストール

1. ubuntu22.04のbox追加

Vagrant box bento/ubuntu-22.04 - Vagrant Cloud

上記サイトからなんか良さそうなboxを探す。

今回は「bento/ubuntu-22.04」を入れた。

最新(202304)はVirtualBox が無いので、バージョンを指定する必要がある。

僕はダウンロードURLを直接指定してやった。

$ vagrant box add bento/ubuntu-22.04 https://app.vagrantup.com/bento/boxes/ubuntu-22.04/versions/202303.13.0/providers/virtualbox.box

環境によってめちゃ時間がかかるので気長に待とう。

box追加が完了したら、一応追加されているか確認

$ vagrant box list
bento/centos7_lamp                   (virtualbox, 0)
bento/ubuntu-22.04                   (virtualbox, 0)
juniper/ffp-12.1X47-D15.4-packetmode (virtualbox, 0.5.0)
rockylinux/9                         (virtualbox, 2.0.0)

2. マシン起動

先ほど追加したboxで初期化

$ vagrant init bento/ubuntu-22.04

次にマシンを起動

$ vagrant up

最初は時間がかかるので、気長に待つ。

完了したら一応状態確認。

$ vagrant status
Current machine states:

default                   running (virtualbox)

「running」になってたらOK

SSHする。

$ vagrant ssh
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed Sep  6 05:41:16 AM UTC 2023

  System load:  0.0                Processes:             148
  Usage of /:   15.8% of 30.34GB   Users logged in:       1
  Memory usage: 19%                IPv4 address for eth0: 10.0.2.15
  Swap usage:   0%                 IPv4 address for eth1: 192.168.33.10

 * Introducing Expanded Security Maintenance for Applications.
   Receive updates to over 25,000 software packages with your
   Ubuntu Pro subscription. Free for personal use.

     https://ubuntu.com/pro


This system is built by the Bento project by Chef Software
More information can be found at https://github.com/chef/bento
Last login: Wed Sep  6 04:32:05 2023 from 10.0.2.2
vagrant@vagrant:~$

とりあえず、アップデートしとこうか。

$sudo apt update

3. pythonあるか確認

デフォルトのpython のバージョンが何か確認

vagrant@vagrant:~$ python -V
-bash: python: command not found
vagrant@vagrant:~$ python3 -V
Python 3.10.12

pyhton3.10 を入れたかったので、そのまま使わせていただこう。

4. pip インストール

pip は入ってないみたいなので、インストールする

$sudo apt install python3-pip

なんかリスタートする?みたいなGUIみたいなの出てきたが、一番上を僕は選んだ。

次にpip のバージョンを確認。

vagrant@vagrant:~$ pip -V
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
vagrant@vagrant:~$ pip3 -V
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

(pipが入ってることも確認できたね)

pip も pip3 も使えるみたい?

python3 と同じくpip3 を使って行こうと思う。

5. selenium インストール

pip でインストールしたらOK

$pip3 install selenium

ちゃんとインストールできたか確認

$pip3 list
.
.
.
selenium               4.12.0

ありそうなのでOK

6. google-chrome インストール

zenn.dev

上記記事を参考にした。

# リポジトリの追加
$ sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'

# 公開鍵の登録
$ sudo wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

# パッケージリストの更新
$ sudo apt update

# インストール
$ sudo apt install google-chrome-stable

# バージョン確認
$ google-chrome --version

ここのバージョン確認の数字はメモしておこう。

あとで使うので。

$ google-chrome --version
Google Chrome 116.0.5845.179

7. chrome のドライバーインストール

さっき見たchromeのバージョンとドライバーのバージョンを併せるらしい。

$ pip install chromedriver-binary==116.0.5845.179

こうか?ってしたらエラーがでた。(以下エラー文)

$ pip3 install chromedriver-binary==116.0.5845.179
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement chromedriver-binary==116.0.5845.179 (from versions: 2.29.1, 2.31.1, 2.3
3.1, 2.34.0, 2.35.0, 2.35.1, 2.36.0, 2.37.0, 2.38.0, 2.39.0, 2.40.1, 2.41.0, 2.42.0, 2.43.0, 2.44.0, 2.45.0, 2.46.0, 70.0.3538.16.0, 7
0.0.3538.67.0, 70.0.3538.97.0, 71.0.3578.30.0, 71.0.3578.33.0, 71.0.3578.80.0, 71.0.3578.137.0, 72.0.3626.7.0, 72.0.3626.69.0, 73.0.36
83.20.0, 73.0.3683.68.0, 74.0.3729.6.0, 75.0.3770.8.0, 75.0.3770.90.0, 75.0.3770.140.0, 76.0.3809.12.0, 76.0.3809.25.0, 76.0.3809.68.0
, 76.0.3809.126.0, 77.0.3865.10.0, 77.0.3865.40.0, 78.0.3904.11.0, 78.0.3904.70.0, 78.0.3904.105.0, 79.0.3945.16.0, 79.0.3945.36.0, 80
.0.3987.16.0, 80.0.3987.106.0, 81.0.4044.20.0, 81.0.4044.69.0, 81.0.4044.138.0, 83.0.4103.14.0, 83.0.4103.39.0, 84.0.4147.30.0, 85.0.4
183.38.0, 85.0.4183.83.0, 85.0.4183.87.0, 86.0.4240.22.0, 87.0.4280.20.0, 87.0.4280.87.0, 87.0.4280.88.0, 88.0.4324.27.0, 88.0.4324.27
.1, 88.0.4324.96.0, 89.0.4389.23.0, 90.0.4430.24.0, 91.0.4472.19.0, 91.0.4472.101.0, 92.0.4515.43.0, 92.0.4515.107.0, 93.0.4577.15.0,
93.0.4577.63.0, 94.0.4606.41.0, 94.0.4606.61.0, 94.0.4606.113.0, 95.0.4638.10.0, 95.0.4638.17.0, 95.0.4638.54.0, 95.0.4638.69.0, 96.0.
4664.18.0, 96.0.4664.35.0, 96.0.4664.45.0, 97.0.4692.20.0, 97.0.4692.36.0, 97.0.4692.71.0, 98.0.4758.48.0, 98.0.4758.80.0, 98.0.4758.1
02.0, 99.0.4844.17.0, 99.0.4844.35.0, 99.0.4844.51.0, 100.0.4896.20.0, 100.0.4896.60.0, 101.0.4951.15.0, 101.0.4951.41.0, 102.0.5005.2
7.0, 102.0.5005.61.0, 103.0.5060.24.0, 103.0.5060.53.0, 103.0.5060.134.0, 103.0.5060.134.1, 104.0.5112.20.0, 104.0.5112.29.0, 104.0.51
12.79.0, 105.0.5195.19.0, 105.0.5195.52.0, 106.0.5249.21.0, 106.0.5249.61.0, 107.0.5304.18.0, 107.0.5304.18.1, 107.0.5304.62.0, 108.0.
5359.22.0, 108.0.5359.71.0, 109.0.5414.25.0, 109.0.5414.74.0, 110.0.5481.30.0, 110.0.5481.77.0, 111.0.5563.19.0, 111.0.5563.41.0, 111.
0.5563.64.0, 112.0.5615.28.0, 112.0.5615.49.0, 113.0.5672.24.0, 113.0.5672.63.0, 114.0.5735.16.0, 114.0.5735.90.0, 115.0.5790.102.0, 1
15.0.5790.170.0, 116.0.5845.42.0, 116.0.5845.49.0, 116.0.5845.62.0, 116.0.5845.82.0, 116.0.5845.96.0, 117.0.5907.0.0, 117.0.5908.0.0,
117.0.5912.0.0, 117.0.5916.0.0, 117.0.5917.0.0, 117.0.5918.0.0, 117.0.5922.0.0, 117.0.5923.0.0, 117.0.5926.0.0, 117.0.5928.0.0, 117.0.
5930.0.0, 117.0.5931.0.0, 117.0.5933.0.0, 117.0.5935.0.0, 117.0.5937.0.0, 117.0.5938.2.0, 117.0.5938.4.0, 117.0.5938.11.0, 117.0.5938.
22.0, 117.0.5938.35.0, 118.0.5940.0.0, 118.0.5941.0.0, 118.0.5943.0.0, 118.0.5950.0.0, 118.0.5952.2.0, 118.0.5954.0.0, 118.0.5955.0.0,
 118.0.5958.0.0, 118.0.5960.0.0, 118.0.5962.0.0, 118.0.5964.0.0, 118.0.5968.0.0, 118.0.5970.0.0, 118.0.5972.0.0, 118.0.5974.0.0, 118.0
.5976.0.0, 118.0.5978.0.0, 118.0.5980.0.0, 118.0.5982.0.0)
ERROR: No matching distribution found for chromedriver-binary==116.0.5845.179

そのバージョンはないよ!ココの中から選んでね!だと推測。

で、一番近いバージョンを使うことにした。

$ pip3 install chromedriver-binary==116.0.5845.96.0
Defaulting to user installation because normal site-packages is not writeable
Collecting chromedriver-binary==116.0.5845.96.0
  Downloading chromedriver-binary-116.0.5845.96.0.tar.gz (5.6 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: chromedriver-binary
  Building wheel for chromedriver-binary (setup.py) ... done
  Created wheel for chromedriver-binary: filename=chromedriver_binary-116.0.5845.96.0-py3-none-any.whl size=6710196 sha256=ce2e97cd080
539fcf070afb718fbef763e55d4c19eb18df49ebf04ff3d1c6edf
  Stored in directory: /home/vagrant/.cache/pip/wheels/24/04/b1/6a955258fb4d8de9b7b03440e7cc481a39d20e172adc6c5f02
Successfully built chromedriver-binary
Installing collected packages: chromedriver-binary
  WARNING: The script chromedriver-path is installed in '/home/vagrant/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed chromedriver-binary-116.0.5845.96.0

なんかいけたみたい。

これでちゃんと動いてくれるかの確認をする。

適当なtest.py ファイルを作って下記コードをぶっこむ。

from selenium import webdriver
from selenium.webdriver import ChromeOptions
import chromedriver_binary

options = ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
# options.add_argument('--disable-gpu')  # ページによって必要な場合がある模様

url = "https://zenn.dev/"
driver = webdriver.Chrome(options=options)
driver.get(url)

print(driver.title)
driver.quit()

そんで、実行する

$ python3 test.py
Zenn|エンジニアのための情報共有コミュニティ

指定したURLの見出しかなんか引っ張ってこれてるから多分OKなんだと思う。

ここまで出来たら環境作り終わり!

最後に

数年前の間隔で「CentOS7」やら「Ubuntu16」やらで環境作ろうとして挫折しかけました。

少し離れただけでなんもわからんになるエンジニア世界。

分かっていたつもりでしたが身をもって体験しました。

相談に乗ってくれた旧友各位に感謝を表します。