Pythonを使用してWordPressのウェブサイト/ブログに記事を自動投稿する

スポンサーリンク

Pythonを使用してWordPressのウェブサイト/ブログに記事を自動投稿してみます。

投稿するためには、python-wordpress-xmlrpcというPythonのライブラリが必要です。事前にインストールしておく必要があります。

■Python

Pythonバージョン

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

■python-wordpress-xmlrpcライブラリをインストールする

まずは投稿するために、python-wordpress-xmlrpcライブラリをインストールします。インストールするために、Windowsのコマンドプロンプトを起動させます。

pip install python-wordpress-xmlrpc

起動後、上記のコマンドを入力し、Enterキーを押します。

Collecting python-wordpress-xmlrpc
Downloading python-wordpress-xmlrpc-2.3.zip (19 kB)
Building wheels for collected packages: python-wordpress-xmlrpc
Building wheel for python-wordpress-xmlrpc (setup.py) … done
Created wheel for python-wordpress-xmlrpc: filename=python_wordpress_xmlrpc-2.3-py3-none-any.whl size=16367 sha256=9c75cf0d8bf84c393b9fa1687edb22b44efafe85760d5dd95f0ce440b2e4f0bc
Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\a8\3c\86\6c9161710c9b4d4230d284d2aadb6967f9be2df33e9246a197
Successfully built python-wordpress-xmlrpc
Installing collected packages: python-wordpress-xmlrpc
Successfully installed python-wordpress-xmlrpc-2.3

インストールが開始され、「Successfully installed python-wordpress-xmlrpc」と表示されると、インストールは完了となります。

■WordPressのウェブサイト/ブログに記事を投稿するスクリプトを作成する

インストールが完了となりましたので、WordPressのウェブサイト/ブログに記事を投稿するスクリプトを作成します。

■コード

from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressPost

#ブログのログイン情報
your_blog = Client('https://laboratory.kazuuu.net/xmlrpc.php', 'ID'', 'パスワード')

myposts=your_blog.call(posts.GetPosts())
post = WordPressPost()
#記事のタイトル
post.title = 'これはテストです'
#投稿のURL
post.slug='test'
#記事(コンテンツ)
post.content = 'テスト記事を投稿しました'
post.id = your_blog.call(posts.NewPost(post))
#投稿ステータス設定
post.post_status = 'publish'
your_blog.call(posts.EditPost(post.id, post))

今回は、当サイト(https://laboratory.kazuuu.net/)にテスト記事を自動で投稿してみます。

■実行

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

実行してみると、当サイト(https://laboratory.kazuuu.net/)にテスト記事が自動で投稿されていることが確認できました。

コメント

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