Skip to content

[Python] Automatically post on WordPress through the “python-wordpress-xmlrpc” package

In the process of using WordPress to run my personal blog, sometimes I want to be able to organize all the articles I have written. Of course, the method I want to use is of course other than manually look slowly, such as obtaining WordPress-related information through some APIs.

After researching, I found several different APIs to use, and finally I chose the python-wordpress-xmlrpc package.

If you are interested in this package, welcome to their PyPI: https://pypi.org/project/python-wordpress-xmlrpc/


Auto Post

If you use this package in the first time, you need to install it:

pip3 install python-wordpress-xmlrpc


And our sample code:

# -*- coding: utf-8 -*-
import json
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost

# Login
with open('account.json', 'r', encoding='utf-8') as f:
    account = json.load(f)

id = account['user']
password = account['password']

url = 'http://clay-atlas.com/xmlrpc.php'

which = 'draft'

wp = Client(url, id, password)

post = WordPressPost()
post.post_status = which
post.title = 'API TEST'
post.content = 'API TEST CONTENT'
post.excerpt = 'API TEST EXCERPT'
post.terms_names = {
    "post_tag": ['Python'],
    "category": ['Python']
}

wp.call(NewPost(post))



Output:

http://clay-atlas.com/xmlrpc.php

Of course I filled in my website to demo, and you should change it to your own.

And the article type I published is “draft”. If you don’t want to post a draft, but publish directly, you should use “publish” instead of “draft”.

Leave a ReplyCancel reply

Exit mobile version