【问题标题】:How do I fetch youtube title and description from given url using python code?如何使用 python 代码从给定的 url 获取 youtube 标题和描述?
【发布时间】:2012-07-04 12:08:16
【问题描述】:

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

【问题讨论】:

  • 最初的信息到底在哪里?您可以根据问题轻松使用某种 HTML 解析器或正则表达式,但很难弄清楚问题是什么。您是否正在浏览该网站并想下载所有描述和标题?你想输入一个视频的 URL 并抓取它吗?

标签: python youtube youtube-api youtube-data-api


【解决方案1】:

第一个答案不再有效,因为 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'] 

【讨论】:

  • 在我使用谷歌开发者控制台创建 API 密钥之后,这对我来说非常有效。
【解决方案2】:

如果您真的想自己编写一个而不被 YouTube 使用您的开发者密钥跟踪,您只需发送请求至:

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

如:https://gdata.youtube.com/feeds/api/videos/fcz_DYms4N4。 它可以根据您的需要返回 XML、JSON 或 JSONP。

【讨论】:

  • 您的链接返回 404。
  • Google 似乎关闭了未经身份验证的 API 访问。
【解决方案3】:

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

欲了解更多信息,请参阅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)

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 2019-03-30
    • 2012-02-08
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多