我正在使用YouTube数据API v3搜索YouTube并解析结果。即使响应上显示4241结果:
resp["pageInfo"]
{'totalResults': 4241, 'resultsPerPage': 50}
我只有几个视频(在这种情况下为49个)。
下面是我用于从搜索API获取所有视频的代码:
def search_youtube(keyword):
base_video_url = 'https://www.youtube.com/watch?v='
base_search_url = 'https://www.googleapis.com/youtube/v3/search?'
# See full sample for function
first_url = base_search_url+'key={}&q={}&part=snippet,id&order=date&maxResults=50'.format(API_KEY, keyword)
video_links = []
url = first_url
while True:
print(len(video_links))
inp = requests.get(url)
resp = json.loads(inp.text)
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
all_videos = search_youtube("ural altay")
我永远无法从中收集所有视频。可能是什么原因?