3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでWordPressに複数の記事を一括で自動投稿するプログラム

Last updated at Posted at 2019-06-21

Pythonを使ってWordPressにフォルダに入っているテキストファイルの複数の記事を一括で自動投稿するプログラムです。

##使い方と詳細な解説
プログラムの使い方と詳細な解説はこちらに書いてあります。
【3秒で記事投稿完了】Pythonでワードプレスに複数の記事を一括で自動投稿してくれるプログラム

##コード本文

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
import os, glob
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

try:
    wp = Client('URL/xmlrpc.php', 'ID', 'PASS')

    for filepath in glob.glob("./posts/*.txt"):
        title = os.path.basename(filepath).replace('.txt','')

        with open(filepath, encoding='shift-jis') as f:
            body = f.read()
            post = WordPressPost()
            post.title = title
            post.content = body
            post.post_status = 'publish'
            # 投稿した記事を下書きのままにしたい場合はpublishをdraftに変更
            # post.post_status = 'draft'

            # tagやcategory割当の場合
            post.terms_names = {
            #     'post_tag': ['Python', 'WordPress'],
                'category': ['WordPress', 'Python']
                }

            wp.call(NewPost(post))

        os.remove(filepath)

except Exception as e:
    print(e)
3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?