Press ESC to close

wordpress使用restful api发送自定义文章类型

代码:


'''
Author: 20004@163.com
Date: 2022-07-17 15:08:36
LastEditors: error: git config user.name && git config user.email & please set dead value or install git
LastEditTime: 2022-07-22 01:58:59
FilePath: PythonCode华为题库小程序数据h12-831-restapi.py
'''
import codecs
import json
import os
import io
import base64
import json
import requests
from pprint import pprint
url = "https://www.xacisco.net/blog/wp-json/wp/v2/docs"
username = "xxxx"
password = "xxxx"
credentials = "{}:{}".format(username, password)
token = base64.b64encode(credentials.encode())
headers = {
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
        "Authorization": "Basic {}".format(token.decode("utf-8")),
        "content-type": "application/json",
    }

def create_wordpress_post(title,content,slug,doc_category,kb_category):
 api_url = "https://www.xacisco.net/blog/wp-json/wp/v2/docs"
 data = {
 'title' : title,
 'status': 'publish',
 'slug' : slug,
 'content': content,
 'doc_category':doc_category,
 'knowledge_base':kb_category
 }
 response = requests.post(api_url,headers=headers, json=data)
 print(response)
 
File_name='h12-831.json'
file=open('./'+File_name,encoding="utf-8")
data=json.loads(file.read())
print(len(data))
################################
for i in range (186,len(data)):
################################
    tiku_bianhao=str(i)
    tiku_wenti=data[i]['question']
    tiku_xuanxiang=''
    
    xuanxiang=json.loads(data[i]['options'])
    tiku_xuanxiang=''
    for j in range(0,len(xuanxiang)):
        #print(xuanxiang[j]['Key'] + xuanxiang[j]['Value'])
        tiku_xuanxiang=tiku_xuanxiang+ ((xuanxiang[j]['Key'] + xuanxiang[j]['Value']))+'n'

    tiku_daan=data[i]['answer']
    title= 'H12-831-v1-' + tiku_bianhao+'-' + tiku_wenti.split('<')[0]
    content=tiku_wenti + 'n'+ tiku_xuanxiang+'n参考答案:'+tiku_daan
    slug='H12-831-v1-'+tiku_bianhao
    create_wordpress_post(title,content,slug,'877','872')
    

发表回复