使用python从多个媒体检索Instagram评论

2024-09-30 07:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用this unofficial API检索特定媒体下的评论。 我稍微修改了代码,这样我就不必每次都更改媒体id来获取评论,所以我的想法基本上是包括一个这样的媒体列表:

media_list = [media_id1, media_id2, ... ]

把它传给一个周期。我的最终输出将是这样的文本文件:

^{pr2}$

我是这样修改原始代码的:

for i in medialist:
    comments = []
    while has_more_comments:
        _ = API.getMediaComments(i,max_id=max_id)
        #comments' page come from older to newer, lets preserve desc order in full list
        for c in reversed(API.LastJson['comments']):
            comments.append(c)
        has_more_comments = API.LastJson.get('has_more_comments',False)
        #evaluate stop conditions
        if count and len(comments)>=count:
            comments = comments[:count]
            #stop loop
            has_more_comments = False
            print "stopped by count"

        #next page
        if has_more_comments:
            max_id = API.LastJson.get('next_max_id','')
            time.sleep(2)

    for c in comments:
        username = c['user']['username']
        text = c['text']
        user = username.encode('utf-8')
        txt = text.encode('utf-8')
        print (i+"\n"+user+": "+txt+"\n")

我的问题是,我只从列表中的第一个媒体标识中获取评论,然后它会给我其他媒体的空列表:

1412361909683907264
[{u'status': u'Active', u'user_id': xxx, u'created_at_utc': xxx, u'created_at': xxx, u'bit_flags': 0, u'comment_like_count': 1, u'did_report_as_spam': False, u'user': {u'username': u'xxx', u'profile_pic_url': u'xxx', u'profile_pic_id': u'xxx', u'full_name': u'xxx', u'pk': xxx, u'is_verified': False, u'is_private': True}, u'content_type': u'comment', u'text': u'When you eat pasta remember me \U0001f602\U0001f602\U0001f602\U0001f602\U0001f44d\U0001f3fb\U0001f4aa\U0001f3fc', u'pk': xxx, u'type': 0, u'has_liked_comment': False}]
1412360153562726838
[]
1412342538912059069
[]
1412336815465111851
[]

问题出在哪里?如果我还没有很明显地注意到我在python上犯的一些错误,那么我就不能原谅我了 谢谢您!在


Tags: textinapiidfalsemorecountusername
1条回答
网友
1楼 · 发布于 2024-09-30 07:21:33

我认为您需要将has_more_comments设置回媒体列表中第一项之后的True。在

for i in medialist: comments = [] has_more_comments = True while has_more_comments: ...

相关问题 更多 >

    热门问题