如何使用python提取youtube视频的标题

时间:2012-01-05 07:15:49

标签: python

我想提取youtube视频的标题,img thumb等?我怎么能在python中做到这一点

3 个答案:

答案 0 :(得分:9)

你绝对想要使用Youtube API,正如C. Reed所说。此代码将显示youtube视频的标题和作者:

 import urllib
 import simplejson

 id = 'KQEOBZLx-Z8'
 url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % id

 json = simplejson.load(urllib.urlopen(url))

 title = json['entry']['title']['$t']
 author = json['entry']['author'][0]['name']

 print "id:%s\nauthor:%s\ntitle:%s" % (id, author, title)

将打印

id:KQEOBZLx-Z8
author:hooplakidz
title:12 Days of Christmas -  Christmas Carol

Youtube API有很多功能,例如,如果您想获取相关视频及其作者,您可以在网址中指定:fields=entry(id),entry(author)

喜欢:http://gdata.youtube.com/feeds/api/videos/4y9kjrVejOI/related?fields=entry(id),entry(author)&alt=json&v=2&prettyprint=true

答案 1 :(得分:4)

您可以使用lxml解析器和xpath表达式来获取所需的内容。 例如,提取YouTube视频的title -

import lxml
from lxml import etree
youtube = etree.HTML(urllib.urlopen("http://www.youtube.com/watch?v=KQEOBZLx-Z8").read()) //enter your youtube url here
video_title = youtube.xpath("//span[@id='eow-title']/@title") //get xpath using firepath firefox addon
print ''.join(video_title)

'12天的圣诞节 - 圣诞节卡罗尔'

现在使用类似的xpath表达式来获取您需要的任何内容。

答案 2 :(得分:2)

您可能还想考虑用于python的YouTube API:http://code.google.com/apis/youtube/1.0/developers_guide_python.html 您可以轻松检索标题,发布用户,发布日期,评级,评论等。