1

我正在使用带有 Python 的 YouTube API。我已经可以收集特定视频的所有评论,包括作者姓名、评论的日期和内容。
我还可以使用单独的一段代码,提取特定作者的个人信息(年龄、性别、兴趣……)。但我不能在一个地方使用它们。即我需要收集视频的所有评论,包括评论作者的姓名以及所有这些作者的个人信息。下面是我开发的代码。但是我收到一个“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)
4

1 回答 1

1

我认为您需要更好地了解 Youtube API 以及一切如何相互关联。我编写了可以处理多种类型的 Feed 或 Entries 的包装类,并“修复”了 gdata 不一致的参数约定。

以下是一些片段,展示了如何在没有太大困难的情况下概括抓取/爬行。

我知道这不是直接回答您的问题,它是更高级的设计,但如果您要进行大量 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/2303769 data/service.py(路由)

于 2012-04-04T16:59:20.790 回答