python: get all youtube video urls of a channel(python:获取频道的所有 youtube 视频网址)
问题描述
我想获取特定频道的所有视频网址.我认为 json 与 python 或 java 将是一个不错的选择.我可以使用以下代码获取最新视频,但是如何获取所有视频链接 (>500)?
I want to get all video url's of a specific channel. I think json with python or java would be a good choice. I can get the newest video with the following code, but how can I get ALL video links (>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
推荐答案
将 max-results 从 1 增加到你想要的任意数量,但要注意他们不建议一次调用太多,并且会将你限制在 50 (https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters).
Increase max-results from 1 to however many you want, but beware they don't advise grabbing too many in one call and will limit you at 50 (https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters).
相反,您可以考虑以 25 个为一组抓取数据,例如,通过更改 start-index 直到没有返回.
Instead you could consider grabbing the data down in batches of 25, say, by changing the start-index until none came back.
这是我将如何做的代码
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
                        这篇关于python:获取频道的所有 youtube 视频网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python:获取频道的所有 youtube 视频网址
				
        
 
            
        基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - 包装空间模型 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				