Debugging API Scrapping - for loop

Hi guys,
I am trying to understand what is wrong with my code, and I have tried multiple things to make it work. So, as a last resort, I kindly ask for some help on how to debug the following code:

for i,second_values in enumerate(second_values):
    url = f"https://www.googleapis.com/youtube/v3/videos?part=statistics,snippet&key={api_key}&id={i}"
    data = json.loads(requests.get(url).text)
    channel_id = data["items"][0]["snippet"]["channelId"]
    published_date = data["items"][0]["snippet"]["publishedAt"]    
    video_title = data["items"][0]["snippet"]["title"]     
    video_description = data["items"][0]["snippet"]["description"]
    likes = data["items"][0]["statistics"]["likeCount"]
    dislikes = data["items"][0]["statistics"]["dislikeCount"]
    views = data["items"][0]["statistics"]["viewCount"]
    comment_count = data["items"][0]["statistics"]["commentCount"]
    row = [video_id,channel_id,published_date,
           video_title,video_description,
           likes,dislikes,views,comment_count]
    data_df.loc[i] = row

but I get this message:

IndexError: list index out of range

NB: everything works fine until cell 14, which I have copied and pasted above. Please, find the full code on the file below.

YouTube Channel - Player1.ipynb (80.4 KB)

Thank you so much!! :raised_hands:

Hi @boemer00,

I haven’t checked your notebook. But from a quick look at the code snippet, the first item in row, video_id isn’t defined in the loop. Also, I would suggest a quick clean up of the code to name variables for data["items"][0]["snippet"] and data["items"][0]["statistics"] to reduce the duplicated code.

Hope this helps!