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

标签 python api comments instagram

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

media_list = [media_id1, media_id2, ... ]

并将其传递给循环。我的最终输出将是一个像这样的文本文件:

media_id1
username1 comment1
username2 comment2
username3 comment3
media_id2
username1 comment1
...

这是我修改原始代码的方式:

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")

我的问题是我只从列表中的第一个 media_id 获得评论,然后它为我提供其他媒体的空列表:

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的能力和经验都很低,并且把它作为一种爱好来学习,所以如果我犯了一些我仍然无法注意到的明显错误,请原谅我 谢谢!

最佳答案

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

对于媒体列表中的 i: 评论=[] 有更多评论 = True 而有更多评论: ...

关于python - 使用 python 从多个媒体检索 Instagram 评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46527490/

相关文章:

python - 使用 Python 连接到 Power BI XMLA 端点

amazon-web-services - 如何使用亚马逊商品广告 API 查找图书的版本?

java - Eclipse 忽略我的评论

python - 将两个字典合并到一个数据框中

Python 获取设备名称

python - 创建 celery 任务然后同步运行

android - 如何在 Android 中嵌入 SoundCloud 播放器?

python - 如何在2个不同帐户的S3存储桶之间复制特定文件

javascript - 如何在生成的 HTML 页面中隐藏 JavaScript 注释?

css - 我如何在 Vim 中注释一行或一行?