gpt4 book ai didi

Python - 使用 xmlrpc api 插入 HTML 内容 tp WordPress

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:44 24 4
gpt4 key购买 nike

我正在尝试通过 XMLRPC 在我的 WordPress 博客中插入 HTML 内容,但如果我插入 HTML - 我收到错误:

raise TypeError, "cannot marshal %s objects" % type(value) TypeError: cannot marshal objects

如果我使用漂白剂(用于清洁标签)-我的页面上会出现带有标签的文本

我的代码

# coding: utf-8
import requests
from bs4 import BeautifulSoup
import bleach
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods import posts


xmlrpc_url = "http://site.ru/xmlrpc.php"
wp_username = "user"
wp_password = "123"
blog_id = ""
client = Client(xmlrpc_url, wp_username, wp_password, blog_id)

url = "https://lifehacker.ru/2016/08/30/finansovye-sovety-dlya-molodyx-par/"
r = requests.get(url)
soup = BeautifulSoup(r.content)

post_title = soup.find("h1")
post_excerpt = soup.find("div", {"class", "single__excerpt"})
for tag in post_excerpt():
for attribute in ["class", "id", "style"]:
del tag[attribute]
post_content = soup.find("div", {"class","post-content"})
for tag in post_content():
for attribute in ["class", "id", "style"]:
del tag[attribute]

post = WordPressPost()
post.title = post_title.text
post.content = post_content
post.id = client.call(posts.NewPost(post))

post.post_status = 'publish'
client.call(posts.EditPost(post.id, post))

如何在我的 WordPress 博客中插入已解析的 HTML 内容?

最佳答案

在漂白并解决问题后使用 .replace。

我的代码

...
post_content_insert = bleach.clean(post_content)

post_content_insert = post_content_insert.replace('&lt;','<')
post_content_insert = post_content_insert.replace('&gt;','>')


post = WordPressPost()
post.title = post_title.text
post.content = post_content_insert
post.id = client.call(posts.NewPost(post))

关于Python - 使用 xmlrpc api 插入 HTML 内容 tp WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com