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

bur*_*ing -1 python

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

sen*_*nte 10

你绝对想要使用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)
Run Code Online (Sandbox Code Playgroud)

将打印

id:KQEOBZLx-Z8
author:hooplakidz
title:12 Days of Christmas -  Christmas Carol
Run Code Online (Sandbox Code Playgroud)

您可以使用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


Ran*_*Rag 5

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

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)
Run Code Online (Sandbox Code Playgroud)

“圣诞节的12天-圣诞颂歌”

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