python使用xmlrpc自动发布文章到wordpress的好例子_wordpress_xmlrpc getpost-CSDN博客

python使用xmlrpc自动发布文章到wordpress的好例子


使用python的 wordpress_xmlrpc模块,这个模块的使用方法请看 https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
这个模块是国外开发的,说明文档也是英文的,看起来吃力的朋友。可以看一下我总结这个模块常用的方法。

安装:

pip3 install python-wordpress-xmlrpc 或者 pip install python-wordpress-xmlrpc 

带有自定义栏目字段的发布文章代码

  1. from wordpress_xmlrpc import Client, WordPressPost

  2. from wordpress_xmlrpc.methods.posts import GetPosts, NewPost

  3. from wordpress_xmlrpc.methods.users import GetUserInfo

  4. from wordpress_xmlrpc.methods import posts

  5. from wordpress_xmlrpc.methods import taxonomies

  6. from wordpress_xmlrpc import WordPressTerm

  7. from wordpress_xmlrpc.compat import xmlrpc_client

  8. from wordpress_xmlrpc.methods import media, posts

  9. import sys

  10. reload(sys)

  11. sys.setdefaultencoding('utf-8')

  12. wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')

  13. post = WordPressPost()

  14. post.title = '文章标题'

  15. post.content = '文章内容'

  16. post.post_status = 'publish'

  17. post.terms_names = {

  18. 'post_tag': ['test', 'firstpost'],

  19. 'category': ['Introductions', 'Tests']

  20. }

  21. post.custom_fields = []

  22. post.custom_fields.append({

  23. 'key': 'price',

  24. 'value': 3

  25. })

  26. post.custom_fields.append({

  27. 'key': 'ok',

  28. 'value': '天涯海角'

  29. })

  30. post.id = wp.call(posts.NewPost(post))

带有特色图像缩略图的发布文章

  1. #coding:utf-8

  2. from wordpress_xmlrpc import Client, WordPressPost

  3. from wordpress_xmlrpc.methods.posts import GetPosts, NewPost

  4. from wordpress_xmlrpc.methods.users import GetUserInfo

  5. from wordpress_xmlrpc.methods import posts

  6. from wordpress_xmlrpc.methods import taxonomies

  7. from wordpress_xmlrpc import WordPressTerm

  8. from wordpress_xmlrpc.compat import xmlrpc_client

  9. from wordpress_xmlrpc.methods import media, posts

  10. import sys

  11. reload(sys)

  12. sys.setdefaultencoding('utf-8')

  13. wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')

  14. filename = './my.jpg' #上传的图片文件路径

  15. # prepare metadata

  16. data = {

  17. 'name': 'picture.jpg',

  18. 'type': 'image/jpeg', # mimetype

  19. }

  20. # read the binary file and let the XMLRPC library encode it into base64

  21. with open(filename, 'rb') as img:

  22. data['bits'] = xmlrpc_client.Binary(img.read())

  23. response = wp.call(media.UploadFile(data))

  24. # response == {

  25. # 'id': 6,

  26. # 'file': 'picture.jpg'

  27. # 'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',

  28. # 'type': 'image/jpeg',

  29. # }

  30. attachment_id = response['id']

  31. post = WordPressPost()

  32. post.title = '文章标题'

  33. post.content = '文章正文'

  34. post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布

  35. post.terms_names = {

  36. 'post_tag': ['test', 'firstpost'], #文章所属标签,没有则自动创建

  37. 'category': ['Introductions', 'Tests'] #文章所属分类,没有则自动创建

  38. }

  39. post.thumbnail = attachment_id #缩略图的id

  40. post.id = wp.call(posts.NewPost(post))

除了可以发布文章,这个模块也可以单独创建新的分类和标签 

  1. #coding:utf-8

  2. from wordpress_xmlrpc import Client, WordPressPost

  3. from wordpress_xmlrpc import WordPressTerm

  4. from wordpress_xmlrpc.methods import taxonomies

  5. import sys

  6. reload(sys)

  7. sys.setdefaultencoding('utf-8')

  8. wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')

  9. #新建标签

  10. tag = WordPressTerm()

  11. tag.taxonomy = 'post_tag'

  12. tag.name = 'My New Tag12'#标签名称

  13. tag.slug = 'bieming12'#标签别名,可以忽略

  14. tag.id = wp.call(taxonomies.NewTerm(tag))#返回的id

  15. #新建分类

  16. cat = WordPressTerm()

  17. cat.taxonomy = 'category'

  18. cat.name = 'cat1'#分类名称

  19. cat.slug = 'bieming2'#分类别名,可以忽略

  20. cat.id = wp.call(taxonomies.NewTerm(cat))#新建分类返回的id

  21. #新建子分类

  22. parent_cat = client.call(taxonomies.GetTerm('category', 20))#20是父分类的id

  23. child_cat = WordPressTerm()

  24. child_cat.taxonomy = 'category'

  25. child_cat.parent = parent_cat.id

  26. child_cat.name = 'My Child Category'#分类名称

  27. child_cat.slug = 'beidongdui'#分类别名,可以忽略

  28. child_cat.id = wp.call(taxonomies.NewTerm(child_cat))#新建分类返回的id