python-2.7 - 当我尝试获取给定关键字的YouTube视频时,为什么我的Python代码返回错误?

标签 python-2.7 youtube youtube-api

每当我尝试运行代码时,都会收到以下错误:“comment_content错误!'nonetype'对象没有属性'href'”我是Python的新手,并且自己没有编写此代码。它是给我使用的。我的理解是它以前运行正常吗?自编写以来,这与YouTube Data 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()

最佳答案

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

希望能有所帮助。

关于python-2.7 - 当我尝试获取给定关键字的YouTube视频时,为什么我的Python代码返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18304370/

相关文章:

python - python 根据另一个列值将多列转换为单列

android - 如何创建从网站检索视频的Android应用

api - 在YouTube API v3中无法获取视频时长

javascript - 对象索引并使用 youtube api 接下来播放

java - 使用 YOUTUBE-API 的视频路径字符串

python - 运行应该接受来自 C 程序的参数的 python 脚本

python - TensorFlow 中的矩阵逆

javascript - 在页面上第二次播放时 YouTube 视频出现在错误的位置

python - Unicode解码错误: 'ascii' codec can't decode '\xc3\xa8' together with '\xe8'

xml - 从 Blogger 导入后,YouTube 嵌入在 WordPress 中不起作用