【问题标题】:How to gather personal information (age,gender..) of all the authors of the comments on a specific video, with Python YouTube API如何使用 Python YouTube API 收集特定视频评论的所有作者的个人信息(年龄、性别……)
【发布时间】:2012-04-04 13:26:42
【问题描述】:

我正在将 YouTube API 与 Python 一起使用。我已经可以收集特定视频的所有 cmets,包括作者姓名、日期和 cmets 内容。
我还可以使用单独的一段代码,提取特定作者的个人信息(年龄、性别、兴趣……)。 但我不能在一个地方使用它们。即我需要收集视频的所有 cmets,包括 cmets 作者的姓名以及所有这些作者的个人信息。 下面是我开发的代码。但是我收到一个“RequestError”,我不知道如何处理以及问题出在哪里。

 import gdata.youtube
import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()
f = open('test1.csv','w')
f.writelines(['UserName',',','Age',',','Date',',','Comment','\n'])

def GetAndPrintVideoFeed(string1):

        yt_service = gdata.youtube.service.YouTubeService()
        user_entry = yt_service.GetYouTubeUserEntry(username = string1)
        X = PrintentryEntry(user_entry)
        return X

def PrintentryEntry(entry):

        # print required fields where we know there will be information
        Y = entry.age.text
        return Y

def GetComment(next1):

        yt_service = gdata.youtube.service.YouTubeService()
        nextPageFeed = yt_service.GetYouTubeVideoCommentFeed(next1)

        for comment_entry in nextPageFeed.entry:

            string1 = comment_entry.author[0].name.text.split("/")[-1]
            Z = GetAndPrintVideoFeed(string1)
            string2 = comment_entry.updated.text.split("/")[-1]
            string3 = comment_entry.content.text.split("/")[-1]

            f.writelines( [str(string1),',',Z,',',string2,',',string3,'\n'])

        next2 = nextPageFeed.GetNextLink().href
        GetComment(next2)

video_id = '8wxOVn99FTE'
comment_feed = yt_service.GetYouTubeVideoCommentFeed(video_id=video_id)

for comment_entry in comment_feed.entry:

        string1 = comment_entry.author[0].name.text.split("/")[-1]
        Z = GetAndPrintVideoFeed(string1)
        string2 = comment_entry.updated.text.split("/")[-1]
        string3 = comment_entry.content.text.split("/")[-1]

        f.writelines( [str(string1),',',Z,',',string2,',',string3,'\n'])

next1 = comment_feed.GetNextLink().href
GetComment(next1)

【问题讨论】:

    标签: python youtube-api


    【解决方案1】:

    我认为您需要更好地了解 Youtube API 以及所有内容之间的关系。我编写了可以处理多种类型的 Feed 或 Entries 的包装类,并“修复”了 gdata 不一致的参数约定。

    这里有一些 sn-ps 展示了如何在没有太大困难的情况下推广抓取/爬取。

    我知道这不是直接回答您的问题,它是更高级的设计,但如果您要进行大量 youtube/gdata 数据提取,则值得考虑。

    def get_feed(thing=None, feed_type=api.GetYouTubeUserFeed):
    
        if feed_type == 'user':
            feed = api.GetYouTubeUserFeed(username=thing)
    
        if feed_type == 'related':
            feed = api.GetYouTubeRelatedFeed(video_id=thing)
    
        if feed_type == 'comments':
            feed = api.GetYouTubeVideoCommentFeed(video_id=thing)
    
        feeds = []
        entries = []
    
        while feed:
            feeds.append(feed)
            feed = api.GetNext(feed)
    
        [entries.extend(f.entry) for f in feeds]
    
        return entries
    

    ...

    def myget(url,service=None):
    
        def myconverter(x):
            logfile = url.replace('/',':')+'.log'
            logfile = logfile[len('http://gdata.youtube.com/feeds/api/'):]
            my_logger.info("myget: %s" % url)
    
            if service == 'user_feed':
                return gdata.youtube.YouTubeUserFeedFromString(x)
    
            if service == 'comment_feed':
                return gdata.youtube.YouTubeVideoCommentFeedFromString(x)
    
            if service == 'comment_entry':
                return gdata.youtube.YouTubeVideoCommentEntryFromString(x)
    
            if service == 'video_feed':
                return gdata.youtube.YouTubeVideoFeedFromString(x)
    
            if service == 'video_entry':
                return gdata.youtube.YouTubeVideoEntryFromString(x)
    
    
        return api.GetWithRetries(url,
                converter=myconverter,
                num_retries=3,
                delay=2,
                backoff=5,
                logger=my_logger
                )
    
    
    mapper={}
    mapper[api.GetYouTubeUserFeed]='user_feed'
    mapper[api.GetYouTubeVideoFeed]='video_feed'
    mapper[api.GetYouTubeVideoCommentFeed]='comment_feed'
    

    https://gist.github.com/2303769data/service.py(路由)

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2021-02-27
      • 1970-01-01
      • 2015-09-24
      • 2012-11-07
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多