6

如何从给定的 url 从 python 代码中获取 youtube 标题和描述。是否有必要为此使用 youtube API?我正在编写一个程序,它需要从给定的 url 中找到生成标题和描述

4

3 回答 3

7

这不是必需的,但它可能比自己编写要快得多且容易得多。

有关更多信息,请参阅https://developers.google.com/youtube/1.0/developers_guide_python

安装gdata模块后,尝试

import gdata.youtube
import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()

# authorize - you need to sign up for your own access key, or be rate-limited
# yt_service.developer_key = 'ABCxyz123...'
# yt_service.client_id = 'My-Client_id'

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

for entry in yt_service.GetTopRatedVideoFeed().entry:
    PrintEntryDetails(entry)
于 2012-07-04T01:44:00.987 回答
6

第一个答案不再有效,因为 V2 API 不再可用,另一个答案是因为 URL 资源不再可用。

这是一个有效的 V3 代码:

from apiclient.discovery import build

DEVELOPER_KEY = 'your api key goes here'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)

ids = '5rC0qpLGciU,LgbuxTfJFr0'
results = youtube.videos().list(id=ids, part='snippet').execute()
for result in results.get('items', []):
    print result['id']
    print result['snippet']['description']
    print result['snippet']['title'] 
于 2016-10-03T07:16:19.757 回答
0

If you really want to write one by yourself without being tracked by YouTube with your developer key, you can simply send a request to:

https://gdata.youtube.com/feeds/api/videos/#{video_id}
https://gdata.youtube.com/feeds/api/videos/#{video_id}?alt=json

Such as: https://gdata.youtube.com/feeds/api/videos/fcz_DYms4N4. It can return XML, JSON, or JSONP depending on your need.

于 2014-02-15T21:06:28.787 回答