YouTube Gdata 在 Python 中没有平均评分
当查找 YouTube 视频的平均评分和评分者数量时,此方法非常有效。
#!/usr/bin/python
import sys
from gdata.youtube.service import YouTubeService
yt = YouTubeService()
entry = yt.GetYouTubeVideoEntry(video_id=sys.argv[1])
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']
print entry.rating.average
print entry.rating.num_raters
但是当视频没有评级时,它会抛出错误。
AttributeError:“NoneType”对象没有属性“average”
什么建议吗?谢谢。
This works perfectly when finding the average rating and number of raters for a youtube video.
#!/usr/bin/python
import sys
from gdata.youtube.service import YouTubeService
yt = YouTubeService()
entry = yt.GetYouTubeVideoEntry(video_id=sys.argv[1])
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']
print entry.rating.average
print entry.rating.num_raters
but when the video has no rating it throws an error.
AttributeError: 'NoneType' object has no attribute 'average'
Any suggestions? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gd:评级已弃用
http://code.google.com/apis/youtube/2.0 /reference.html#youtube_data_api_tag_gd: rating
无论如何,你应该检查是否存在 raiting
gd:rating is deprecated
http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_gd:rating
Anyway you should check if raiting exists