2013-08-18 58 views
0

每當我嘗試運行我的代碼,我收到以下錯誤:「comment_content錯誤!'nonetype'對象沒有屬性'href'」我是Python的新手,並沒有自己寫這個代碼;它給了我使用。我的理解是它之前運作正常?這是否與自編寫YouTube數據API後的更改有關?爲什麼當我嘗試爲給定關鍵字提取YouTube視頻時,我的Python代碼返回錯誤?

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() 

回答

0

comment_content.GetNextLink()變成None時發生錯誤。爲了解決這個問題,替換:

while indexh < 10: 

有:

while indexh < 10 and comment_content: 

同時更換:

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

有:

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

希望有所幫助。

相關問題