0

每当我尝试运行我的代码时,都会收到以下错误:“comment_content 错误!'nonetype' 对象没有属性 'href'”我是 Python 新手,我自己没有编写此代码;它是给我使用的。我的理解是它之前运行正常?这是否与 YouTube 数据 API 自编写以来的更改有关?

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

1 回答 1

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

希望有帮助。

于 2013-08-18T22:54:53.207 回答