YouTube中的平均评分

时间:2011-11-11 00:32:57

标签: python youtube youtube-api

如何使用Python YouTube GData API调用gd:rating XML文件?

我将YouTube video_id作为变量,我知道可以将其返回:

<gd:rating average='4.553648' max='5' min='1' numRaters='233' rel='http://schemas.google.com/g/2005#overall'/>

我无法弄清楚如何在Python中做到这一点。

1 个答案:

答案 0 :(得分:1)

previous question中,您已经演示了检索video_id条目的代码。

运行python

>>> from gdata.youtube.service import YouTubeService
>>> yt = YouTubeService()
>>> entry = yt.GetYouTubeVideoEntry(video_id='pP9VjGmmhfo')
>>> entry.rating
<gdata.youtube.Rating object at 0x2722cd0>
>>> print entry.rating
<?xml version='1.0' encoding='UTF-8'?>
<ns0:rating average="4.4662576" max="5" min="1" numRaters="652" rel="http://schemas.google.com/g/2005#overall" xmlns:ns0="http://schemas.google.com/g/2005" />
>>> entry.rating.average
'4.4662576'
>>> entry.rating.numRaters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Rating' object has no attribute 'numRaters'
>>> dir(entry.rating)
['FindExtensions', 'ToString', '_AddMembersToElementTree', '_BecomeChildElement', '_ConvertElementAttributeToMember', '_ConvertElementTreeToMember', '_HarvestElementTree', '_ToElementTree', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_attributes', '_children', '_namespace', '_tag', 'average', 'extension_attributes', 'extension_elements', 'max', 'min', 'num_raters', 'text']
>>> entry.rating.num_raters
'652'