好的,我尝试将此循环应用于Youtube API,因为我有一个我想要使用的700个频道ID的列表,而Youtube API的限制为30 :
我有这个错误:
Traceback (most recent call last):
File "C:\Users\William\Documents\failed atttempts\New trial\loopy version.py", line 177, in <module>
id='x')
File "C:\Users\William\Documents\failed atttempts\New trial\loopy version.py", line 33, in channels_list_by_id
(results['items'][0]['snippet']['title'],
IndexError: list index out of range
这基本上意味着我没有回应。
我尝试循环使用channels.list方法的代码:
channel_id = [the great long list of 700 ids in '' separated by commas]
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
list_of_lists = [(chunks(channel_id, 30))]
for list in list_of_lists:
for x in list:
channels_list_by_id(service,
part='snippet,contentDetails,statistics',
id='x')
我不知道要在id中输入什么,所以我只是把x放在x之前,因为x就像我列表中的每个列表一样。
工作方法只是:
channels_list_by_id(service,
part='snippet,contentDetails,statistics',
id='whatever channel ID I put here usually I put in 30 ids
separated by commas')
我的循环好吗?如何让id与我的列表相同?
另外,如果你想看到channels_list_by_id被定义为:
def channels_list_by_id(service, **kwargs):
results = service.channels().list(
**kwargs
).execute()
是的,kwargs就是那里,因为它在示例代码idk中是什么。