从Youtube视频中抓取元素

2024-04-25 08:40:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从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>")

Tags: thereforreadhtmllinkitemsurllib2
1条回答
网友
1楼 · 发布于 2024-04-25 08:40:08

使用google's 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

相关问题 更多 >

    热门问题