Sunday, 17 August 2014

Pass gdata.youtube.YouTubeVideoCommentEntry in xml.etree.ElementTree



I am using this code. Via this for loop:



for comment in comments_generator(client, VIDEO_ID):
author_name = comment.author[0].name.text
text = comment.content.text
print("{}: {}".format(author_name, text))


you can retrieve some useful information about comments. If you add print comment you may see that there is a relevant tag <ns1:channelId>...</ns1:channelId>. I want to access the content of this tag but when you wrote comment.channelId this message returned:



AttributeError: 'YouTubeVideoCommentEntry' object has no attribute 'channelId'


So, because anything else failed, I have decided to use xml.etree.ElementTree. Unfortunately, when I append the commands:



stree = ET.parse(comment) root = tree.getroot()


in the for loop this error returned:



TypeError: coercing to Unicode: need string or buffer, YouTubeVideoCommentEntry found


which is correct because the type of the comment (type(comment)) variable is:



<class 'gdata.youtube.YouTubeVideoCommentEntry'>


Since now I have tried to transform comment in string:



import xml.etree.ElementTree as ET
from xml.etree.ElementTree import XML, fromstring, tostring
def ToString(self, string_encoding='UTF-8'):
"""Converts the Atom object to a string containing XML."""
return tostring(self._ToElementTree(), encoding=string_encoding)


but relevant errors returned.



  1. How can we do this execution: ET.parse(comment) correct?

  2. Are there any other alternatives to access channelId?


Thanks.


No comments:

Post a Comment