我正在尝试在youtube数据api中获取nextPageToken
下面是我的代码,我希望得到所有nextpageToken
。
在循环中,我想重新分配nextPageToken
def get_next_videos():
while True:
r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY)
json_data = r.json()
nextPageToken = json_data.get("nextPageToken")
second_r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY+"\
&pageToken="+nextPageToken)
json_data = second_r.json()
nextPageToken = json_data.get("nextPageToken")
third_r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY+"&\
pageToken="+nextPageToken)
json_data = third_r.json()
nextPageToken = json_data.get("nextPageToken")
fourth_r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY+"\
&pageToken="+nextPageToken)
json_data = third_r.json()
nextPageToken = json_data.get("nextPageToken")
答案 0 :(得分:4)
假设您在最后一页时不会返回nextPageToken
,那么您可以这样做:
# First request
r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY)
json_data = r.json()
nextPageToken = json_data.get("nextPageToken")
# Retrieve all the rest of the pages
while nextPageToken:
r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY+"&pageToken="+nextPageToken)
json_data = r.json()
nextPageToken = json_data.get("nextPageToken")
当然,在循环中,您可能还希望收集您在某些适当的数据结构(列表,字典等)中检索的数据。