我最近开始使用Blogger API编写一个简单的客户端来做一些我在Python中实现客户端的基本帖子,并使用Blogger Developer's Guide中的示例代码逐字登录,获取博客ID,并创建一个新的帖子。我运行了脚本,一切都很顺利,直到我到达这一行:
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
我收到了错误消息:
Traceback (most recent call last):
File "cs1121post.py", line 38, in <module>
cs1121post()
File "cs1121post.py", line 33, in cs1121post
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
File "/usr/local/lib/python2.7/dist-packages/gdata/service.py", line 1236, in Post
media_source=media_source, converter=converter)
File "/usr/local/lib/python2.7/dist-packages/gdata/service.py", line 1322, in PostOrPut
headers=extra_headers, url_params=url_params)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 93, in optional_warn_function
return f(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/atom/service.py", line 176, in request
content_length = CalculateDataLength(data)
File "/usr/local/lib/python2.7/dist-packages/atom/service.py", line 736, in CalculateDataLength
return len(str(data))
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 377, in __str__
return self.ToString()
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 374, in ToString
return ElementTree.tostring(self._ToElementTree(), encoding=string_encoding)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 369, in _ToElementTree
self._AddMembersToElementTree(new_tree)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 331, in _AddMembersToElementTree
member._BecomeChildElement(tree)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 357, in _BecomeChildElement
self._AddMembersToElementTree(new_child)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 342, in _AddMembersToElementTree
ExtensionContainer._AddMembersToElementTree(self, tree)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 224, in _AddMembersToElementTree
tree.text = self.text.decode(MEMBER_STRING_ENCODING)
AttributeError: 'list' object has no attribute 'decode'
我认为ElementTree在这里有问题。我通过
安装了ElementTreesudo python setup.py install
如果重要的话。 ElementTree和Python v2.7.1之间是否存在一些已知的不兼容性?这是否发生在其他任何人身上,你是如何让它发挥作用的?如果您需要任何其他信息,请回复该主题。所有相关的源代码基本上只是上面提到的开发人员指南中的示例代码。我根本没有修改过它(甚至不是变量名)。任何意见都非常感谢。
答案 0 :(得分:1)
堆栈跟踪实际上非常清楚:你在列表而不是树元素上调用decode()
。尝试从列表中获取第一个元素并在其上调用decode()
:
firsttext = self.text[0].decode(MEMBER_STRING_ENCODING)