首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python从多个媒体检索Instagram评论

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

Stack Overflow用户
提问于 2017-10-02 22:24:52
回答 1查看 1.8K关注 0票数 1

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

代码语言:javascript
复制
media_list = [media_id1, media_id2, ... ]

并将其传递给一个循环。我的最终输出将是一个文本文件,如下所示:

代码语言:javascript
复制
media_id1
username1 comment1
username2 comment2
username3 comment3
media_id2
username1 comment1
...

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

代码语言:javascript
复制
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获得评论,然后它给我提供了其他媒体的空列表:

代码语言:javascript
复制
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的能力和经验都很低,只是把它当作一种爱好来学习,所以如果我犯了一些明显的错误,我仍然没有注意到,请原谅,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-10-02 23:03:23

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46527490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档