Skip to main content
 首页 » 编程设计

python之发送帖子包含带有python的非拉丁字符

2025年05月04日15mengfanrong

我有一个 WordPress 网站,我想使用 python 发送一些特定的帖子。当我的帖子不包含非拉丁字符时,我没有问题,我可以轻松发布它们。

但是,当我在我的帖子中使用像 'ğ' 或 'ş' 这样的字符时,它会给我一个解析错误。格式不正确错误。由于我网站的语言,我必须使用 iso-8859-9 编码,无论我尝试了什么,我都无法解决这个问题。

这是我的代码:

# -*- coding: iso-8859-9 -*- 
from wordpress_xmlrpc import Client 
from wordpress_xmlrpc.methods import posts 
from wordpress_xmlrpc import WordPressPost 
 
 
wp = Client('http://websiteurl/xmlrpc.php', 'username', 'pass') 
#posts = wp.call(posts.GetPosts()) 
#print posts[0] 
 
data="Güneş doğudan doğar" 
 
post = WordPressPost() 
post.title = 'My Post Title' 
post.content = data 
post.id = wp.call(posts.NewPost(post)) 
 
post.post_status = 'publish' 
wp.call(posts.EditPost(post.id, post)) 

请您参考如下方法:

data 已经编码为 iso-8859-9,因此您需要对其进行decode() 以传递它通过到 WordPress。问题行是 post.content = data。您需要将其更改为:

post.content = data.decode("iso-8859-9")