Why is my Python code returning an error when I try to fetch YouTube videos for a given keyword?
Question: Whenever I try to run my code, I receive the following error: "comment_content
error! 'nonetype' object has no attribute 'href'" I am new to Python, and did
not write this code myself; it was given to me to use. My understanding is
that it was functioning properly before? Could this have to do with changes in
the YouTube Data API since it was written?

    
    
    import pdb
    import gdata.youtube
    import gdata.youtube.service
    import codecs
    import time
    
    client = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()
    
    
    ### the input words are here
    query.vq = "4b hair"
    #######
    
    # the out put file are here
    viewFile = codecs.open('views4b_hair.csv', 'w')
    commentFile=codecs.open('comments4b_hair.csv', 'w')
    ##########
    
    
    
    query.max_results = 50
    query.start_index = 0
    query.safesearch = "moderate"
    #query.format = 5
    query.orderby = "relevance"
    #query.author = "hawaiinani"
    #pdb.set_trace() 
    
    
    
    
    for i in range(19):
        #pdb.set_trace() 
        query.start_index=str(int(query.start_index)+50)
        feed = client.YouTubeQuery(query)
        print len(feed.entry)
    
        youtubeid=[]
        youtubetitle=[]
        for entry in feed.entry:
    
            #youtubetitle.append(entry.title.text)
            youtubeid.append(entry.id.text[38:])
            print entry.id.text[38:],i
            try:
               entry_comment = client.GetYouTubeVideoEntry(video_id=entry.id.text[38:])
               comment_feed = client.GetYouTubeVideoCommentFeed(video_id=entry.id.text[38:])            
               viewFile.write(','.join([entry.id.text[38:],entry_comment.published.text,
                                   str(entry_comment.media.duration.seconds), str(entry_comment.statistics.view_count),comment_feed.total_results.text,entry_comment.media.title.text.decode('ascii', errors='ignore').encode('ascii', 'ignore')]) + '\n')
                #videop.append("%s, %s,%s, %s, %s, %s" % (search_result["id"]["videoId"],entry.published.text,
                #                       entry.media.duration.seconds, entry.statistics.view_count,comment_feed.total_results.text,entry.media.title.text))
                #  
               #time.sleep(3)
            except Exception, ex:
                print 'View_content Error', ex
                time.sleep(10)
            try:
               comment_content = client.GetYouTubeVideoCommentFeed(video_id=entry.id.text[38:])
               indexh=0
               #while comment_content:
               while indexh<10:
                   indexh=indexh+1
                   for comment_entry in comment_content.entry:
                       pubText = comment_entry.published.text
                       #print pubText
    
                       titleText = comment_entry.content.text.decode('ascii', errors='ignore').encode('ascii', 'ignore')
                       #print titleText
                       #print 'Got title'
                       #pubText, titleText = comment_entry.published.text, comment_entry.title.text
                       commentFile.write(','.join([entry.id.text[38:],pubText,titleText]) + '\n'+'\n')
                       #commentFile.write(u',')
                       #commentFile.write(pubText + u',')
                       #print 'About to write title'
                       #print titleText
    
                       #print 'Wrote title'
    
                       #commentlist.append("%s, %s,%s" % (search_result["id"]["videoId"],pubText, titleText)) 
                   comment_content=client.Query(comment_content.GetNextLink().href)
                   #time.sleep(3)
               #time.sleep(3)
            except Exception, ex:
                print 'Comment_content Error!', ex
                time.sleep(5)
    
    
    
    
    
        #pdb.set_trace()
    viewFile.close()
    commentFile.close()
    


Answer: The error occurs when `comment_content.GetNextLink()` becomes `None`. In order
to fix it, replace:

    
    
    while indexh < 10:
    

with:

    
    
    while indexh < 10 and comment_content:
    

also replace:

    
    
    comment_content=client.Query(comment_content.GetNextLink().href)
    

with:

    
    
    next_link = comment_content.GetNextLink()
    if next_link:
        comment_content = client.Query(next_link.href)
    else:
        comment_content = None
    

Hope that helps.

