python:获取频道的所有YouTube视频网址

问题描述 投票:6回答:4

我想获取特定频道的所有视频网址。我认为使用python或java的json将是一个不错的选择。我可以使用以下代码获取最新视频,但如何获得所有视频链接(> 500)?

import urllib, json
author = 'Youtube_Username'
inp = urllib.urlopen(r'http://gdata.youtube.com/feeds/api/videos?max-results=1&alt=json&orderby=published&author=' + author)
resp = json.load(inp)
inp.close()
first = resp['feed']['entry'][0]
print first['title'] # video title
print first['link'][0]['href'] #url
python youtube youtube-api
4个回答
10
投票

将最大结果从1增加到你想要的多少,但要注意,他们不建议在一次通话中抓取太多,并将限制在50(https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters)。

相反,您可以考虑通过更改start-index直到没有回来来批量抓取25个数据。

编辑:这是我将如何做的代码

import urllib, json
author = 'Youtube_Username'

foundAll = False
ind = 1
videos = []
while not foundAll:
    inp = urllib.urlopen(r'http://gdata.youtube.com/feeds/api/videos?start-index={0}&max-results=50&alt=json&orderby=published&author={1}'.format( ind, author ) )
    try:
        resp = json.load(inp)
        inp.close()
        returnedVideos = resp['feed']['entry']
        for video in returnedVideos:
            videos.append( video ) 

        ind += 50
        print len( videos )
        if ( len( returnedVideos ) < 50 ):
            foundAll = True
    except:
        #catch the case where the number of videos in the channel is a multiple of 50
        print "error"
        foundAll = True

for video in videos:
    print video['title'] # video title
    print video['link'][0]['href'] #url

6
投票

基于此处和其他地方的代码,我编写了一个小脚本来执行此操作。我的脚本使用了Youtube API的v3,但没有达到Google为搜索设置的500个结果限制。

代码可以在GitHub上找到:https://github.com/dsebastien/youtubeChannelVideosFinder


4
投票

在youtube API更改后,max k。的答案不起作用。作为替代,以下功能提供了给定频道中的YouTube视频列表。请注意,您需要一个API Key才能工作。

import urllib
import json

def get_all_video_in_channel(channel_id):
    api_key = YOUR API KEY

    base_video_url = 'https://www.youtube.com/watch?v='
    base_search_url = 'https://www.googleapis.com/youtube/v3/search?'

    first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(api_key, channel_id)

    video_links = []
    url = first_url
    while True:
        inp = urllib.urlopen(url)
        resp = json.load(inp)

        for i in resp['items']:
            if i['id']['kind'] == "youtube#video":
                video_links.append(base_video_url + i['id']['videoId'])

        try:
            next_page_token = resp['nextPageToken']
            url = first_url + '&pageToken={}'.format(next_page_token)
        except:
            break
    return video_links

1
投票

独立的做事方式。没有api,没有速率限制。

import requests
username = "marquesbrownlee"
url = "https://www.youtube.com/user/username/videos"
page = requests.get(url).content
data = str(page).split(' ')
item = 'href="/watch?'
vids = [line.replace('href="', 'youtube.com') for line in data if item in line] # list of all videos listed twice
print(vids[0]) # index the latest video

以上代码将仅删除有限数量的视频网址,最多可达60.如何获取频道中存在的所有视频网址。你能建议吗?

以上代码段仅显示列出两次的所有视频的列表。并非所有视频网址都在频道中。

© www.soinside.com 2019 - 2024. All rights reserved.