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

Why does Youtube API give me different results for some channels than they actually are?

$
0
0

I have a script that gets information from Youtube using their API and saves it to excel file.

The script runs normally for some channels and for some it does not, e.g. when I used it for BBC and CNN channel it got all of the information that it needed, but when I tried it on MrBeast or Tech With Tim channel, it only returned me 3 videos for MrBeast and 2 for Tech With Tim, even though both channels have more videos.

What could be the problem?

Here is the full code:

import pandas as pdfrom googleapiclient.discovery import buildapi_key = 'YOUR_API_KEY_HERE'  # Replace with your API keyyoutube = build('youtube', 'v3', developerKey=api_key)# Retrieve the channel ID for the given usernamechannel_request = youtube.channels().list(    part='snippet,contentDetails',    forUsername='BBCNews')channel_response = channel_request.execute()# Extract channel username and IDchannel_username = channel_response['items'][0]['snippet']['title']channel_id = channel_response['items'][0]['id']# Initialize variables for paginationnext_page_token = Noneall_videos = []# Retrieve all videos from the channelwhile True:    videos_request = youtube.search().list(        part='snippet',        channelId=channel_id,        type='video',        order='date',        maxResults=50,  # Maximum results per page        pageToken=next_page_token    )    videos_response = videos_request.execute()    all_videos.extend(videos_response['items'])    next_page_token = videos_response.get('nextPageToken')    if not next_page_token:        break  # Break the loop if there are no more pages# Initialize lists to store extracted informationtitles = []published_dates = []likes = []views = []descriptions = []urls = []# Extract information from each videofor video in all_videos:    video_id = video['id']['videoId']    video_title = video['snippet']['title']    video_description = video['snippet']['description']    video_published_at = video['snippet']['publishedAt']    video_url = f'https://www.youtube.com/watch?v={video_id}'    # Retrieve statistics for each video    video_statistics_request = youtube.videos().list(        part='statistics',        id=video_id    )    video_statistics_response = video_statistics_request.execute()    video_statistics = video_statistics_response['items'][0]['statistics']    video_likes = int(video_statistics.get('likeCount', 0))    video_views = int(video_statistics.get('viewCount', 0))    # Append extracted information to lists    titles.append(video_title)    published_dates.append(video_published_at)    likes.append(video_likes)    views.append(video_views)    descriptions.append(video_description)    urls.append(video_url)# Create a DataFrame from the listsdata = {'Title': titles,'Published Date': published_dates,'Likes': likes,'Views': views,'Description': descriptions,'URL': urls}df = pd.DataFrame(data)# Save DataFrame to Excel file with channel username as filenameexcel_file_path = f'{channel_username}_videos.xlsx'df.to_excel(excel_file_path, index=False)print("Excel file saved successfully.")

Viewing all articles
Browse latest Browse all 3638

Trending Articles



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