问题
How do I fetch youtube title and description from python code from the given url. Is it necessory to use youtube API for it? I am writing a program which need to find generate title and description from given url
回答1:
It is not necessary, but it is probably significantly quicker and easier than writing your own.
For more information, see https://developers.google.com/youtube/1.0/developers_guide_python
After installing the gdata
module, try
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)
回答2:
Both answers no longer work the first because the V2 API is no longer available the other one because the URL resource is no longer available.
This is a V3 code that is working:
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']
回答3:
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.
来源:https://stackoverflow.com/questions/11321429/how-do-i-fetch-youtube-title-and-description-from-given-url-using-python-code