如何通过http请求在Instagram中获取关注者和关注列表 [英] How to get followers and following list in Instagram via http requests

查看:334
本文介绍了如何通过http请求在Instagram中获取关注者和关注列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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']

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

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?

推荐答案

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

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_hash and variables, where variables is 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 after set to the end_cursor in the previous response dictionary.

或者, Instaloader 库提供了一种方便的登录方式,然后以编程方式访问配置文件的关注者和关注者列表

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

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

除了username之外,在为每个跟随者返回的Profile实例中定义了full_nameuseridfollowed_by_viewer等属性.

Besides username, the attributes full_name, userid, followed_by_viewer and many more are defined in the Profile instance that is returned for each followee.

这篇关于如何通过http请求在Instagram中获取关注者和关注列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆