如何使用Python提取YouTube视频标题
我想提取一个YouTube视频的标题、缩略图等信息,怎么用Python来实现呢?
3 个回答
2
你可能还想了解一下适用于Python的YouTube API:http://code.google.com/apis/youtube/1.0/developers_guide_python.html。通过这个工具,你可以轻松获取视频的标题、发布用户、发布时间、评分、评论等等信息。
11
你肯定想使用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)
6
你可以使用 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 Days of Christmas - Christmas Carol'
然后,你可以用类似的 xpath 表达式来获取你想要的其他内容。