1.安装插件
在/wp-content/plugins/目录执行以下命令
$ sudo git clone https://github.com/WP-API/Basic-Auth.git
2.Python脚本
import requests
url = 'http://www.ziyuanwang.online/wp-json/wp/v2/posts'
user = 'admin'
password = 'xxxxx'
headers = {
'Content-Type': 'application/json',
}
post = {
'title': 'Hello World',
'status': 'publish',
'content': 'This is my first post published from Python.',
'author': '1', # Assuming '1' is your user ID in WordPress
}
response = requests.post(url, json=post, headers=headers, auth=(user, password))
if response.status_code == 201:
print("Post published successfully")
else:
print("Failed to publish post")
致谢
https://blog.csdn.net/cunjie3951/article/details/106923078?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7EPaidSort-1-106923078-blog-78706423.235%5Ev43%5Epc_blog_bottom_relevance_base6&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7EPaidSort-1-106923078-blog-78706423.235%5Ev43%5Epc_blog_bottom_relevance_base6&utm_relevant_index=1