我想提取youtube视频的标题,img thumb等?我怎么能在python中做到这一点
答案 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)
答案 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 您可以轻松检索标题,发布用户,发布日期,评级,评论等。