如下图所示,在使用metaWeblog.newPost方法远程发布文章时出现TypeError: cannot marshal set objects报错,在此分享解决方法:
代码如下:
post = {
'blog_id':'1',
'username':wp_username,
'password':wp_pass,
'content':{
'title': get_content[0],
'description': get_content[3],
'mt_keywords': get_content[1],
'mt_excerpt':get_content[3].text[:45],
'mt_allow_comments': '1',
'mt_allow_pings': '1',
'wp_slug': '',
'categories':[get_content[2]],
'wp_password':'',
'publish': True,
}
}
response = wp_client.metaWeblog.newPost(post['blog_id'], post['username'], post['password'], post['content'],True)报警图片如下

原因及解决:metaWeblog.newPost这个方法支持数字、字符串、list等格式的数据,但不支持Set格式的数据。根据报错信息可知,传入的文章内容结构中有的数据格式不正确。解决思路:通过print(type(变量))的方法,查看各个变量类型,最后发现get_content[3]格式不正确,将之强制转换为字符串类型,再次运行程序,报警消失了!

发表评论 取消回复