【エラーが発生】Pythonを使用してInstagramに写真を自動投稿する

Pythonを使用してInstagramに写真を自動投稿してみます。

■Python

Pythonバージョン

今回のPythonのバージョンは、「3.8.2」を使用しています。(Windows10)

■instabotモジュールをインストールする

Pythonを使用してInstagramに写真を自動投稿するために、まずはinstabotモジュールをインストールします。なお、instabotモジュールはPythonの標準ライブラリではありません。

pip install instabot

instabotモジュールをインストールするために、Windowsのコマンドプロンプトを起動します。起動後、上記のコマンドを入力し、Enterキーを押します。

C:\Users\user>pip install instabot
Collecting instabot
Downloading instabot-0.117.0.tar.gz (84 kB)
|████████████████████████████████| 84 kB 452 kB/s
Requirement already satisfied: certifi>=2019.11.28 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (2020.4.5.1)
Requirement already satisfied: chardet>=3.0.4 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (3.0.4)
Requirement already satisfied: future>=0.18.2 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (0.18.2)
Collecting huepy>=1.2.1
Downloading huepy-1.2.1.tar.gz (15 kB)
Requirement already satisfied: idna>=2.8 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (2.9)
Collecting pysocks>=1.7.1
Downloading PySocks-1.7.1-py3-none-any.whl (16 kB)
Requirement already satisfied: pytz>=2019.3 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (2020.1)
Requirement already satisfied: requests>=2.22.0 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (2.23.0)
Collecting requests-toolbelt>=0.9.1
Downloading requests_toolbelt-0.9.1-py2.py3-none-any.whl (54 kB)
|████████████████████████████████| 54 kB 734 kB/s
Collecting responses>=0.10.9
Downloading responses-0.12.0-py2.py3-none-any.whl (15 kB)
Requirement already satisfied: schedule>=0.6.0 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (0.6.0)
Requirement already satisfied: six>=1.14.0 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (1.14.0)
Collecting tqdm>=4.41.1
Downloading tqdm-4.50.2-py2.py3-none-any.whl (70 kB)
|████████████████████████████████| 70 kB 720 kB/s
Requirement already satisfied: urllib3>=1.25.7 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (1.25.9)
Collecting mock>=3.0.5
Downloading mock-4.0.2-py3-none-any.whl (28 kB)
Collecting moviepy>=1.0.1
Downloading moviepy-1.0.3.tar.gz (388 kB)
|████████████████████████████████| 388 kB 1.6 MB/s
Requirement already satisfied: Pillow>=6.2.2 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from instabot) (7.2.0)
Collecting pytest>=4.6.9
Downloading pytest-6.1.1-py3-none-any.whl (272 kB)
|████████████████████████████████| 272 kB 3.2 MB/s
Requirement already satisfied: decorator<5.0,>=4.0.2 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from moviepy>=1.0.1->instabot) (4.4.2)
Collecting proglog<=1.0.0
Downloading proglog-0.1.9.tar.gz (10 kB)
Requirement already satisfied: numpy>=1.17.3 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from moviepy>=1.0.1->instabot) (1.18.4)
Collecting imageio<3.0,>=2.5
Downloading imageio-2.9.0-py3-none-any.whl (3.3 MB)
|████████████████████████████████| 3.3 MB 3.2 MB/s
Collecting imageio_ffmpeg>=0.2.0
Downloading imageio_ffmpeg-0.4.2-py3-none-win_amd64.whl (22.6 MB)
|████████████████████████████████| 22.6 MB 3.2 MB/s
Requirement already satisfied: colorama; sys_platform == “win32” in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from pytest>=4.6.9->instabot) (0.4.3)
Requirement already satisfied: toml in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from pytest>=4.6.9->instabot) (0.10.0)
Collecting py>=1.8.2
Downloading py-1.9.0-py2.py3-none-any.whl (99 kB)
|████████████████████████████████| 99 kB 1.9 MB/s
Requirement already satisfied: packaging in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from pytest>=4.6.9->instabot) (20.3)
Collecting iniconfig
Downloading iniconfig-1.0.1-py3-none-any.whl (4.2 kB)
Collecting attrs>=17.4.0
Downloading attrs-20.2.0-py2.py3-none-any.whl (48 kB)
|████████████████████████████████| 48 kB 1.4 MB/s
Collecting atomicwrites>=1.0; sys_platform == “win32”
Downloading atomicwrites-1.4.0-py2.py3-none-any.whl (6.8 kB)
Requirement already satisfied: pluggy<1.0,>=0.12 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from pytest>=4.6.9->instabot) (0.13.1)
Requirement already satisfied: pyparsing>=2.0.2 in c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages (from packaging->pytest>=4.6.9->instabot) (2.4.7)
Building wheels for collected packages: instabot, huepy, moviepy, proglog
Building wheel for instabot (setup.py) … done
Created wheel for instabot: filename=instabot-0.117.0-py3-none-any.whl size=101285 sha256=5d3468837a5ac02ef0a3253d06ac1193f1426216f0819e01d604cd7a1f4944a6
Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\89\ff\9e\82ddf7bb4048272eeab5e16158be4617c0ae79a2ad62c902a4
Building wheel for huepy (setup.py) … done
Created wheel for huepy: filename=huepy-1.2.1-py3-none-any.whl size=27002 sha256=6d4630c49fe832a32165b89fc6ebd2018a5f3bc4ca47fbb4583d4ac44d652fdc
Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\1d\83\e3\de56afd11275b45edb9bcb4e435988c4c398a988da530d6995
Building wheel for moviepy (setup.py) … done
Created wheel for moviepy: filename=moviepy-1.0.3-py3-none-any.whl size=110731 sha256=a548145cca08c9b447daf6dae3b24926c06e35e953ccbd67bcf2cef2982ebf19
Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\e4\a4\db\0368d3a04033da662e13926594b3a8cf1aa4ffeefe570cfac1
Building wheel for proglog (setup.py) … done
Created wheel for proglog: filename=proglog-0.1.9-py3-none-any.whl size=6151 sha256=0cb470df81db9696d1348efa8c233cc10f4fb3d7d2eb0cf567e8651016bf2909
Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\22\82\ff\56950e4b223a69eda3a2bdeaef2ae4b274c3d1ced9b5814c99
Successfully built instabot huepy moviepy proglog
ERROR: responses 0.12.0 has requirement urllib3>=1.25.10, but you’ll have urllib3 1.25.9 which is incompatible.
Installing collected packages: huepy, pysocks, requests-toolbelt, responses, tqdm, mock, proglog, imageio, imageio-ffmpeg, moviepy, py, iniconfig, attrs, atomicwrites, pytest, instabot
WARNING: The script tqdm.exe is installed in ‘C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
WARNING: The scripts imageio_download_bin.exe and imageio_remove_bin.exe are installed in ‘C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
Attempting uninstall: py
Found existing installation: py 1.8.1
Uninstalling py-1.8.1:
Successfully uninstalled py-1.8.1
WARNING: The scripts py.test.exe and pytest.exe are installed in ‘C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts’ 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 atomicwrites-1.4.0 attrs-20.2.0 huepy-1.2.1 imageio-2.9.0 imageio-ffmpeg-0.4.2 iniconfig-1.0.1 instabot-0.117.0 mock-4.0.2 moviepy-1.0.3 proglog-0.1.9 py-1.9.0 pysocks-1.7.1 pytest-6.1.1 requests-toolbelt-0.9.1 responses-0.12.0 tqdm-4.50.2
WARNING: You are using pip version 20.1; however, version 20.2.3 is available.
You should consider upgrading via the ‘C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe -m pip install –upgrade pip’ command.

Enterキーを押すと、インストールが開始されます。その後、「Successfully installed」と表示されインストールは完了したようですが、「ERROR: responses 0.12.0 has requirement urllib3>=1.25.10, but you’ll have urllib3 1.25.9 which is incompatible.」というエラーが表示されています。

pip install --upgrade urllib3

このエラーの原因は恐らくurllib3モジュールのバージョンに関するもの(https://github.com/coursera-dl/coursera-dl/issues/667)なので、モジュールをアップグレードするために上記のコマンドを入力し、Enterキーを押します。押すと、アップグレードを開始されます。

■instabotモジュールを使用しInstagramに写真を自動投稿する

インストールが完了しましたので、instabotモジュールを使用しInstagramに写真を自動投稿するスクリプトを書いていきます。

■コード

from instabot import Bot

bot = Bot()

bot.login(username="ユーザーネーム",
password="パスワード")
bot.upload_photo(r"C:\Users\user\test\test.png",
caption= "これはテストの投稿です。#テスト")

開発者向けドキュメントを参考にしています。(https://instagrambot.github.io/docs/en/For_developers.html

■投稿のために用意した画像

■実行

今回書いたスクリプトを「post_Instagram.py」という名前でコマンドプロンプトから実行してみます。

2020-10-11 09:18:19,939 – INFO – Instabot version: 0.117.0 Started
2020-10-11 09:18:19,941 – INFO – Recovery from C:\Users\user/config/ユーザーネーム_uuid_and_cookie.json: COOKIE True – UUIDs True – TIMING, DEVICE and …
– user-agent=Instagram 117.0.0.28.123 Android (28/9.0; 420dpi; 1080×1920; OnePlus; ONEPLUS A3003; OnePlus3; qcom; en_US; 180322800)
– phone_id=425ee633-600f-4e52-bf7a-e6100e55f412
– uuid=5b5102ea-607c-4608-b934-16603f9d4f77
– client_session_id=e4dbda5d-fd2e-4fc8-beda-88e00ceeeca7
– device_id=android-c83fac20eda7b3b8
2020-10-11 09:18:19,942 – INFO – LOGIN FLOW! Just logged-in: False
2020-10-11 09:18:26,500 – INFO – Logged-in successfully as ‘ユーザーネーム’!
FOUND: w:1080 h:1080 r:1.0
2020-10-11 09:18:30,501 – ERROR – Photo Upload failed with the following response: <Response [400]>
2020-10-11 09:18:30,502 – INFO – Photo ‘C:\Users\user\test\test.png’ is not uploaded.
2020-10-11 09:18:30,507 – INFO – Total requests: 31

コマンドプロンプトで上記のような結果が出力されました。Instagramに正常にログイン(Logged-in successfully)できているようですが、「ERROR – Photo Upload failed with the following response: <Response [400]>(エラー-写真のアップロードが次の応答で失敗しました:<応答[400]>)」というエラーが出力されています。

エラーの原因を探り解決方法を探しましたが、見つかりませんでした。

解決方法は見つかりませんでしたが、GitHubで、bot.upload_photo()関数を使い、400エラーを返すという質問が海外ユーザーによって投稿されているのを発見しました。(https://github.com/ohld/igbot/issues/1111)、(https://github.com/ohld/igbot/issues/1211

Instagramのセキュリティによってブロックされている可能性もあるので、このライブラリ自体が使えないような感じがします。このライブラリ自体、使わない方が良い。

コメント

タイトルとURLをコピーしました