2011-11-11 103 views
0

如何使用Python YouTube GData API調用gd:rating XML文件?YouTube平均評分於Python

我在YouTube VIDEO_ID作爲一個變量,我知道這是可能返回此:

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

我只是無法弄清楚如何做到這一點在Python。

回答

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' 
+0

謝謝JF現在它只是搞清楚在什麼地方 '目錄(entry.rating)' https://www.stypi.com/davidneudorfer/youtube。 py –

+0

@David Neudorfer:如果你不知道「Run'python'」是什麼意思,那麼至少應該從書[Learn Python The Hard Way]中學習[Exercise 0:The Setup](http://learnpythonthehardway.org/) )。 – jfs

+0

no no我有上述腳本的工作在: https://www.stypi.com/davidneudorfer/watch_likes_dislikes.py 但我不知道如何將它與我的主腳本合併: https:// www .stypi.com/davidneudorfer/youtube.py 任何建議都會很棒。 –