折腾:
【已解决】用Python发布印象笔记帖子内容到WordPress网站
期间,想要去用Python把印象笔记的帖子发送到WordPress中。
其中先要搞清楚,用什么库,以及调用哪些接口,以及什么参数。
python WordPress 接口
好像不是很好的样子
JSON API – WordPress plugin | WordPress.org
“This plugin has been closed as of August 7, 2019 and is not available for download. Reason: Security Issue.”
由于安全原因,不给下载和使用了。
都是说用的是:xmlrpc
不是我希望的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
>>> 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的,用起来很麻烦。
继续找找:
对应函数:
- 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的字段:
- 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)
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))
有人裸写操作mysql的,可以的。
没有提到如何上传图片。
听说支持REST API去发布帖子,所以去试试:
【已解决】用Python通过WordPress的REST的API发布文章post
【总结】
此处用Python调用接口创建post的逻辑是:
调用接口是:
- 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文章时加上分类和标签