蟒蛇:获得一个频道的所有YouTube视频网址(python: get all youtube vi

2019-07-22 01:10发布

我希望得到一个特定频道的所有视频网址。 我认为JSON与Python或Java将是一个不错的选择。 我可以用下面的代码的最新视频,但我怎么能得到所有的视频链接(> 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

Answer 1:

增加从1最大结果到然而,许多你想要的,但要小心,他们不建议抓住一个电话太多,并会限制你在50( https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters )。

相反,你可以考虑下抓取的数据批次的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


Answer 2:

根据这里找到的代码,并在其他一些地方,我写了一个小脚本,做这个。 我的脚本使用的Youtube API的v3和不打对抗,谷歌已经为搜索的500个结果的限制。

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



Answer 3:

YouTube的API变更后,公司的MAXķ。答案不工作。 作为替换,该函数提供了以下的在一个给定的信道的YouTube视频的列表。 请注意,你需要一个API密钥为它工作。

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


Answer 4:

做事独立的方式。 没有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如何抓住所有的视频网址这是目前在通道中。 你可以请建议。

这上面的代码片段将只显示其中的两次列出所有视频列表。 并不是所有的视频网址在渠道。



文章来源: python: get all youtube video urls of a channel