最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】用Python发布帖子到WordPress用什么库和调用哪些接口

印象笔记 crifan 3065浏览 0评论
折腾:
【已解决】用Python发布印象笔记帖子内容到WordPress网站
期间,想要去用Python把印象笔记的帖子发送到WordPress中。
其中先要搞清楚,用什么库,以及调用哪些接口,以及什么参数。
python WordPress 接口
wordpress-api · PyPI
GitHub – derwentx/wp-api-python: A Python wrapper for the WooCommerce API.
好像不是很好的样子
python实现WordPress文章发布(二):Methods类发布文章 – 云+社区 – 腾讯云
JSON API强大的WordPress做API接口插件 – Su_tianbiao的专栏 – CSDN博客
JSON API – WordPress plugin | WordPress.org
https://wordpress.org/plugins/json-api/
“This plugin has been closed as of August 7, 2019 and is not available for download. Reason: Security Issue.”
由于安全原因,不给下载和使用了。
【自动发文】python实现WordPress文章发布(一):环境配置与安装 – 简书
都是说用的是:xmlrpc
使用xmlrpc接口控制wordpress – Readventurer
Python wordpress自动发布 | 勤奋的小青蛙
不是我希望的rest api的,不过对于xmlrpc,倒是给出了如何上传图片
                up_filename = r'F:/aikanmeizi/' + prow[3] + "/" + prow[0]
                print "up_filename:" + up_filename
                with open(up_filename, 'rb') as img:
                    data['bits'] = xmlrpc_client.Binary(img.read())
                response = wp.call(media.UploadFile(data))
                attachment_id = response['id']
                post.thumbnail = attachment_id
Client — python-wordpress-xmlrpc 2.3 documentation
Overview — python-wordpress-xmlrpc 2.3 documentation
>>> from wordpress_xmlrpc import Client, WordPressPost
>>> from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
>>> from wordpress_xmlrpc.methods.users import GetUserInfo

>>> wp = Client('http://mysite.wordpress.com/xmlrpc.php', 'username', 'password')
>>> wp.call(GetPosts())
[<WordPressPost: hello-world (id=1)>]

>>> wp.call(GetUserInfo())
<WordPressUser: max>

>>> post = WordPressPost()
>>> post.title = 'My new title'
>>> post.content = 'This is the body of my new post.'
>>> post.terms_names = {
>>>   'post_tag': ['test', 'firstpost'],
>>>   'category': ['Introductions', 'Tests']
>>> }
>>> wp.call(NewPost(post))
5
貌似用起来还行,不算很复杂。
不过,是xmlrpc的,用起来很麻烦。
继续找找:
python-wordpress-xmlrpc/docs/ref at master · maxcutler/python-wordpress-xmlrpc · GitHub
python-wordpress-xmlrpc/client.rst at master · maxcutler/python-wordpress-xmlrpc · GitHub
python-wordpress-xmlrpc/methods.rst at master · maxcutler/python-wordpress-xmlrpc · GitHub
对应函数:
  • GetPost(post_id[, fields])
  • NewPost(content)
  • EditPost(post_id, content)
  • DeletePost(post_id)
以及:
  • GetTerm(taxonomy, term_id)
  • NewTerm(term)
  • EditTerm(term_id, term)
  • DeleteTerm(taxonomy, term_id)
以及post的字段:
python-wordpress-xmlrpc/wordpress.rst at master · maxcutler/python-wordpress-xmlrpc · GitHub
  • WordPressPost
    • id
    • user
    • date (datetime)
    • date_modified (datetime)
    • slug
    • post_status
    • title
    • content
    • excerpt
    • link
    • comment_status
    • ping_status
    • terms (list of :class:`WordPressTerm`s)
    • terms_names (dict)
    • custom_fields (dict)
    • enclosure (dict)
    • password
    • post_format
    • thumbnail
    • sticky
    • post_type
  • WordPressPostType
    • name
    • label
    • labels (dict)
    • cap (dict)
    • hierarchical
    • menu_icon
    • menu_position
    • public
    • show_in_menu
    • taxonomies (list)
    • is_builtin
    • supports (list)
  • WordPressTerm
    • id
    • group
    • taxonomy
    • taxonomy_id
    • name
    • slug
    • description
    • parent
    • count (int)
python-wordpress-xmlrpc/examples.rst at master · maxcutler/python-wordpress-xmlrpc · GitHub
python-wordpress-xmlrpc/posts.rst at master · maxcutler/python-wordpress-xmlrpc · GitHub
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts

client = Client(...)
posts = client.call(posts.GetPosts())
# posts == [WordPressPost, WordPressPost, ...]
和:
from wordpress_xmlrpc import WordPressPost

post = WordPressPost()
post.title = 'My post'
post.content = 'This is a wonderful blog post about XML-RPC.'
post.id = client.call(posts.NewPost(post))

# whoops, I forgot to publish it!
post.post_status = 'publish'
client.call(posts.EditPost(post.id, post))
XML-RPC WordPress API/Posts « WordPress Codex
wp.newPost
深入Wordpress数据库结构,研究一下用Python程序发帖 • 遁一的博客空间
有人裸写操作mysql的,可以的。
GitHub – daijingjing/wordpress_python_api: 通过tornado给wordpress建立一个HTTP+JSON的API数据接口
「人生苦短我用Python」之Python自动发布Markdown文章到WordPress网站 – 微普拉斯
没有提到如何上传图片。
WordPress Metadata API 元数据接口 – 拾拾博客
听说支持REST API去发布帖子,所以去试试:
【已解决】用Python通过WordPress的REST的API发布文章post
【总结】
此处用Python调用接口创建post的逻辑是:
调用接口是:
Posts | REST API Handbook | WordPress Developer Resources
  • POST /wp/v2/posts
    • header
      • Authorization
        • ‘Bearer your_jwt_token’
          • jwt的token是
            • 如何得到的
              • WordPress安装插件:JWT Authentication for WP REST API
              • 然后再去更新配置后
                • 允许调用REST接口:/jwt-auth/v1
              • 然后(用Postman)调用接口:
                • POST https://www.crifan.com/wp-json/jwt-auth/v1/token
              • 返回的token
            • 且每过一段时间会过期,需要重新生成
          • 具体细节详见:
            • 【已解决】给crifan.com的WordPress网站REST的API添加JWT的token认证
            • 【已解决】WordPress的jwt-auth用Postman去测试生成token和验证token
      • Accept
        • ‘application/json’
    • 参数
      • 常见参数和举例
        • title
          • ‘【记录】Mac中用pmset设置GPU显卡切换模式’
        • content
          • ‘<html>\n <div>\n  折腾:\n </div>\n <div>\n  【已解决】Mac Pro 2018款发热量大很烫非常烫\n </div>\n <div>\n  期间,…performance graphic cards\n    </li>\n   </ul>\n  </ul>\n </ul>\n <div>\n  <br/>\n </div>\n</html>’
        • date
          • ‘2020-08-17T10:16:34’
        • slug
          • ‘on_mac_pmset_is_used_set_gpu_graphics_card_switching_mode’
        • status
          • ‘draft’
        • format
          • ‘standard’
        • categories
          • [1374]
        • tags
          • [1367, 13224, 13225, 13226]
      • 更多参数详见
具体代码详见:
【已解决】用Python发布印象笔记帖子内容到WordPress网站
【已解决】给Python发布印象笔记帖子内容到WordPress文章时加上分类和标签

转载请注明:在路上 » 【已解决】用Python发布帖子到WordPress用什么库和调用哪些接口

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.156 seconds, using 22.19MB memory