从YouTube视频抓取元素
我想从YouTube视频中提取一些信息,比如标题、观看次数等等。我用BeautifulSoup这个工具来做这个,但我希望能让这个过程更快。以下是我的代码:
#for the title
from BeautifulSoup import BeautifulSoup
html = re.findall('content=.*>\n\n',urllib2.urlopen(link).read())
soup = BeautifulSoup(html)
print soup.prettify()
#for the number of views
soup0 = BeautifulSoup(urllib2.urlopen(link).read())
for items in soup0.findAll('strong'):
if re.match("^[0-9]*$", str(items).strip("<strong>").rstrip("</strong>")):
viewcount=str(strongs).strip("<strong>").rstrip("</strong>")
1 个回答
7
使用 谷歌的YouTube API。
这是他们示例的一部分:
def PrintEntryDetails(entry):
print 'Video title: %s' % entry.media.title.text
print 'Video published on: %s ' % entry.published.text
print 'Video description: %s' % entry.media.description.text
print 'Video category: %s' % entry.media.category[0].text
print 'Video tags: %s' % entry.media.keywords.text
print 'Video watch page: %s' % entry.media.player.url
print 'Video flash player URL: %s' % entry.GetSwfUrl()
print 'Video duration: %s' % entry.media.duration.seconds