Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Youtube Data API Search - Next page token is None after 11 pages

$
0
0

I am trying to capture channel ids of all channels with q = "news" category; there are total of 36714 results but for some reason, the next page token keeps showing None after a few pages for me.Can someone please check my code, am I doing something wrong

#CodeChannelCategory = "news"import pandas as pdfrom csv import writerdef append_list_as_row(file_name, list_of_elem):    with open(file_name, 'a+', newline='') as write_obj:        # Create a writer object from csv module        csv_writer = writer(write_obj)        # Add contents of list as last row in the csv file        csv_writer.writerow(list_of_elem)def get_channel_data(youtube,ChannelCategory):    page_iteration = 0    all_data = []    request = youtube.search().list(                part="snippet",                channelType="any",                maxResults=50,                q= ChannelCategory,                order="date",                type="channel"                )    response = request.execute()    #CSV creation    append_list_as_row('channel_data1.csv', ['channelId', 'description', 'channelTitle'])    #loop through 50 items -- Following in the list    for item in response['items']:        data = [            item['snippet']['channelId'],            item['snippet']['description'],            item['snippet']['title']            ]        #all_data.append(data) adding in csv        append_list_as_row('channel_data1.csv', data)    next_page_token = response.get('nextPageToken')    print('After first 50 - next token is ', next_page_token)    more_pages = True    while more_pages:        if next_page_token is None:            more_pages = False        else:            request = youtube.search().list(                        part="snippet",                        channelType="any",                        maxResults=50,                        q= ChannelCategory,                        order="date",                        pageToken = next_page_token,                        type="channel"                        )            response = request.execute()            for item in response['items']:                data = [                    item['snippet']['channelId'],                    item['snippet']['description'],                    item['snippet']['title']                    ]                #all_data.append(data) adding in csv                append_list_as_row('channel_data1.csv', data)            next_page_token = response.get('nextPageToken')            page_iteration = page_iteration +1            print(next_page_token)            if page_iteration == 20:                more_pages = False   print(next_page_token)       return(pd.DataFrame(all_data))

the output that I get of above code is below

After first 50 - next token is CDIQAACGQQAACJYBEAACMgBEAACPoBEAACKwCEAACN4CEAACJADEAACMIDEAACPQDEAACKYEEAANoneNone


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>