Python 如何通过 http 请求在 Instagram 中获取关注者和关注列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37658723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:45:00  来源:igfitidea点击:

How to get followers and following list in Instagram via http requests

pythoninstagram

提问by Ilya Kryukov

I'm looking for a possibility to get a followers and following list in JSON format via web request (in the same way as on the Instagram web site). For example, I can login via requests, and get user info:

我正在寻找通过网络请求获得 JSON 格式的关注者和关注者列表的可能性(与 Instagram 网站上的方式相同)。例如,我可以通过请求登录,并获取用户信息:

def get_user_info(self, user_name):
    url = "https://www.instagram.com/" + user_name + "/?__a=1"
    try:
        r = requests.get(url)
    except requests.exceptions.ConnectionError:
        print 'Seems like dns lookup failed..'
        time.sleep(60)
        return None
    if r.status_code != 200:
        print 'User: ' + user_name + ' status code: ' + str(r.status_code)
        print r
        return None
    info = json.loads(r.text)
    return info['user']

I tried to see what request chrome send to server, but was unsuccessful. The question is: how to prepare a similar get or post request to retrieve followers list without the Instagram API?

我试图查看 chrome 发送到服务器的请求,但没有成功。问题是:如何在没有 Instagram API 的情况下准备类似的获取或发布请求来检索关注者列表?

回答by aandergr

GraphQL queries with query_hash= "58712303d941c6855d4e888c5f0cd22f" (followings) and "37479f2b8209594dde7facb0d904896a" (followers) return this information. With being logged in, do a GET query to instagram.com/graphql/query with parameters query_hashand variables, where variablesis a JSON-formatted set of variables id(user id, as in the return dictionary of your get_user_info()function), first(a page length, it seems the current maximum is 50) and in subsequent requests afterset to the end_cursorin the previous response dictionary.

带有query_hash=“58712303d941c6855d4e888c5f0cd22f”(以下)和“37479f2b8209594dde7facb0d904896a”(以下)的GraphQL 查询返回此信息。登录后,使用参数query_hash和对 instagram.com/graphql/query 执行 GET 查询variables,其中variables是一组 JSON 格式的变量id(用户 ID,如get_user_info()函数的返回字典中所示),first(页面长度,它似乎当前的最大值是 50),并且在随后的请求中after设置为end_cursor之前的响应字典中的 。

Alternatively, the Instaloaderlibrary provides a convenient way to login and then programmatically access a profile's followers and followings list.

或者,Instaloader库提供了一种方便的登录方式,然后以编程方式访问个人资料的关注者和关注者列表。

import instaloader

# Get instance
L = instaloader.Instaloader()

# Login or load session
L.login(USER, PASSWORD)        # (login)
L.interactive_login(USER)      # (ask password on terminal)
L.load_session_from_file(USER) # (load session created w/
                               #  `instaloader -l USERNAME`)

# Obtain profile metadata
profile = instaloader.Profile.from_username(L.context, PROFILE)

# Print list of followees
for followee in profile.get_followees():
    print(followee.username)

# (likewise with profile.get_followers())

Besides username, the attributes full_name, userid, followed_by_viewerand many more are defined in the Profileinstance that is returned for each followee.

此外username,属性full_nameuseridfollowed_by_viewer而且有很多的定义Profile则返回为每个关注者的实例。

回答by Вадим Виноградов

Easy(just replace _a with __a)

简单(只需将 _a 替换为 __a)

'https://www.instagram.com/'+user_name+'/followers/?_a=1'
'https://www.instagram.com/'+user_name+'/following/?_a=1'