通过api接口上传到wordpress
如何确定api接口的categories
SELECT t.*, tt.*
FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt
ON t.term_id = tt.term_id
WHERE tt.taxonomy IN ('category')
ORDER BY t.name ASC
通过python上传wordpress代码可参考
# -*- coding:utf-8 -*-
import re
import requests
import json
import time
from numpy import *
site="http://bigdata.ministep.cn"
def get_token():
session = requests.Session()
url = '{site}/wp-json/jwt-auth/v1/token'.format(site=site)
data = {
'username':"xxxxx",
'password':"xxxx"
}
headers = {'user-agent': 'Mozolla/5.0',
}
resp = session.post(url, data=data, headers=headers, timeout=3335) # 请求
r = json.loads(resp.content)
return r
def wp_post(post):
r = get_token()
token = r["data"]['token']
session = requests.Session()
url = '{site}/wp-json/wp/v2/posts'.format(site=site)
data = {
'date': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
'date_gmt': time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()),
'slug': '',
'status': 'publish', #'draft'
'password': '', #用于保护对内容和摘录的访问的密码。
'title': post.get("title"),
'content': post.get("content"),
#'author': post.get("author"),
'author ': post.get("2"),
'excerpt': post.get("brief"), #摘要
'featured_media': '662',
'comment_status': 'open',
'ping_status': 'closed',
'format': 'standard',
'meta': [],
'sticky': False, # 置顶
'template': '',
'categories': '108', # 1 未分类 108-新闻联播
'tags': '1'
}
headers = {'user-agent': 'Mozolla/5.0',
'Authorization': 'Bearer ' + token
}
resp = session.post(url, data=data, headers=headers, timeout=3335) # 请求
print (resp.text)
return resp.json()
def wp_post_update(post_update_obj):
r = get_token()
token = r["data"]['token']
post=post_update_obj
session = requests.Session()
url = '{site}/wp-json/wp/v2/posts/{id}'.format(site=site,id=post.get("id"))
data = {
'id':post.get("id"),
'date': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
'date_gmt': time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()),
'slug': '',
'status': 'publish', #'draft'
'password': '', #用于保护对内容和摘录的访问的密码。
'title': post.get("title"),
'content': post.get("content"),
#'author': post.get("author"),
'author ': post.get("2"),
'excerpt': post.get("brief"), #摘要
'featured_media': '662',
'comment_status': 'open',
'ping_status': 'closed',
'format': 'standard',
'meta': [],
'sticky': False, # 置顶
'template': '',
'categories': '108', # 1 未分类 108-新闻联播
'tags': '1'
}
headers = {'user-agent': 'Mozolla/5.0',
'Authorization': 'Bearer ' + token
}
resp = session.post(url, data=data, headers=headers, timeout=3335) # 请求
print (resp.text)
return resp.json()
if __name__=='__main__':
post = {
"title":"py_demo_v203",
"content":"py_content",
'brief':"content-brief",
}
# wp_post(post)
post_update_obj = {
"id":"631",
"title":"py_demo_v2_update_create",
"content":"py_content-203",
'brief':"content-brief",
}
wp_post_update(post_update_obj)
参考:
通过python使用wordpress的api接口写入文章 – 大数据 (ministep.cn)