2012-01-05 31 views

回答

4

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

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表达式来获取自己需要的任何内容。

8

你一定要使用YouTube API,正如C.里德所说。此代码将向您展示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,例如,如果你想只得到相关的视频和它们的作者,你可以在URL中指定: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

相关问题