使用频道ID列表,例如:
channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB','UC6nSFpjSEBUCZ5t-N3Ra3-HB','UC6nrst90rsd3Z5t-N3Ra3-HC','UC6nSFpjd329th0rt-tuTH3-HA']
我想使用YouTube Data API v3检索所有这些频道的50个最新视频上传,尽可能少地使用http请求并尽可能缩短时间。
我目前的做法是:
from apiclient.discovery import build youtube = build('youtube', 'v3', developerKey=key) channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB', 'UC6nSFpjSEBUCZ5t-N3Ra3-HB', 'UC6nrst90rsd3Z5t-N3Ra3-HC', 'UC6nSFpjd329th0rt-tuTH3-HA'] videos_by_channel = {} for channel_id in channel_ids: search_response = youtube.search().list(part="id", type='video', order='date', channelId=channel_id, maxResults=50).execute() videoIds = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videoIds.append(search_result['id']['videoId']) search_response = youtube.videos().list( id=",".join(videoIds), part="id,snippet" ).execute() videos_by_channel[channel_id] = search_response它的工作原理,但使用大量的服务器调用,它不是很快。 我已阅读文档,但找不到更快的方法,有什么想法?
Using a list of channel IDs such as:
channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB', 'UC6nSFpjSEBUCZ5t-N3Ra3-HB', 'UC6nrst90rsd3Z5t-N3Ra3-HC', 'UC6nSFpjd329th0rt-tuTH3-HA']
I want to retrieve the 50 most recent video uploads for all those channels using the YouTube Data API v3, using as few http requests and as little time as possible.
The way I'm currently doing it is:
from apiclient.discovery import build youtube = build('youtube', 'v3', developerKey=key) channel_ids = ['UC6nSFpj9HTCZ5t-N3Ra3-HB', 'UC6nSFpjSEBUCZ5t-N3Ra3-HB', 'UC6nrst90rsd3Z5t-N3Ra3-HC', 'UC6nSFpjd329th0rt-tuTH3-HA'] videos_by_channel = {} for channel_id in channel_ids: search_response = youtube.search().list(part="id", type='video', order='date', channelId=channel_id, maxResults=50).execute() videoIds = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videoIds.append(search_result['id']['videoId']) search_response = youtube.videos().list( id=",".join(videoIds), part="id,snippet" ).execute() videos_by_channel[channel_id] = search_responseIt works but uses a lot of server calls and it isn't exactly fast. I've read the documentation but can't find a faster method, any ideas?
最满意答案
您可以通过发送批量请求发送多个请求
You can send multiple request with Sending Batch Requests
更多推荐
发布评论