python - Python 到 wordpress
问题描述
我已经编写了这段代码,用于将带有元数据的 cpt 从 python 发送到 WordPress,这是我已经证明的 4 种不同场景之一,实际上我需要使用外部变量将带有一些元字段的帖子从 python 发送到 WordPress设置标题和产品价格和产品规格,我需要通过获取请求向邮递员请求的 JSON 文件是
[
{
"id": 3693,
"date": "2021-07-14T20:00:35",
"date_gmt": "2021-07-14T18:00:35",
"guid": {
"rendered": "http://www.mydomain.it/acme_product/spec-3/"
},
"modified": "2021-07-20T01:58:48",
"modified_gmt": "2021-07-19T23:58:48",
"slug": "spec-3",
"status": "publish",
"type": "acme_product",
"link": "http://www.mydomain.it/acme_product/spec-3/",
"title": {
"rendered": "spec"
},
"featured_media": 0,
"template": "",
"product_price": null,
"product_spec": null,
"_links": {
"self": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/acme_product/3693"
}
],
"collection": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/acme_product"
}
],
"about": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/types/acme_product"
}
],
"wp:attachment": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/media?parent=3693"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
当我实际使用我的 python 代码 wordpress 接受该帖子但不填写产品价格和产品规格的字段时,一些想法,我该怎么办?谢谢大家
from __future__ import print_function
import base64
import sys
from datetime import datetime
import json
import requests
user= 'username'
password='auth
url = 'http://www.mydomain.it/wp-json/wp/v2/'
d= sys.argv
a_dict={}
b_dict={}
data_string = user + ':' + password
token = base64.b64encode(data_string.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8')}
date="2021-07-14T20:00:35"
title=d[1]
slug= d[1]
status= "publish"
author="1"
##excerpt="exc post"
##format="standard"
product_price= d[2]
product_spec=d[3]
for var in ["product_price","product_spec"]:
b_dict[var]= eval(var)
print (b_dict[var])
meta= b_dict[var]
for variable in ["date", "title", "slug","status","author","product_price","product_spec"]:
a_dict[variable] = eval(variable)
r = requests.post(url + 'acme_product', headers=headers, json=a_dict)
print(r.content.decode('utf-8'))
print('Your post is published on ' + json.loads(r.content.decode('utf-8'))['link'])
解决方案
推荐阅读
- c# - ASP.Net/C# 如何存储方法字符串返回以供以后在另一个方法中使用?
- javascript - TypeError:当结果为空时,this.state.candidate.map 不是函数
- typescript - Extract / Guard promise, pull Promise
来自混合工会 - javascript - 如何为 amcharts 地图添加标题?
- python-3.x - 使用`ray.put`创建对象后如何显式释放对象?
- r - 在 R 中将时间范围转换为可读格式
- python - 在 Selenium 中设置用户数据/配置文件不起作用。硒蟒
- python - 递归协同树生成算法
- python - Python/PyTables:数组的不同列是否可以有不同的数据类型?
- google-sheets - 连接 ArrayFormula 内的列,不包括某些值