Python - 使用xmlrpc api

时间:2016-08-30 19:35:38

标签: python wordpress beautifulsoup xml-rpc

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

  

引发TypeError,"无法编组%s对象" %type(value)TypeError:   不能编组物品

如果我使用漂白剂(用于干净的标签) - 我的页面上有标签的文字

我的代码

# 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内容?

1 个答案:

答案 0 :(得分:1)

在漂白和修复问题后使用.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))