如何在Python中没有API的情况下从用户名ID中获取instagram用户名?

Lor*_*ord 2 python json python-2.7 instagram instagram-api

我试图找到一种方法从 Python 中的用户名 ID 获取 Instagram 用户名。到目前为止我发现的是:

https://api.instagram.com/v1/users/[USER-ID]?access_token=[YOUR-ACCESS-TOKEN]
Run Code Online (Sandbox Code Playgroud)

我有一个令牌号,但它只适用于添加到沙箱的用户。对于其他用户,我收到以下错误:

{"meta": {"code": 400, "error_type": "APINotFoundError", "error_message": "this user does not exist"}}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我正在寻找解决方案数小时,但没有运气。有没有办法在不使用令牌的情况下达到相同的结果?

非常感谢!

Zoh*_*Ali 6

就我而言,我从 tagFeed 获取数据,所以我有图片代码。如果您有任何用户帖子的图片代码,那么您可以获得这样的用户名

https://www.instagram.com/p/picCODE/?__a=1

example: https://www.instagram.com/p/BaGce-NlMg7/?__a=1
Run Code Online (Sandbox Code Playgroud)

它将返回一个带有所有者的 JSON - 用户名


aan*_*rgr 5

将浏览器指向https://i.instagram.com/api/v1/users/USERID/info/(将 USERID 替换为用户 ID)。它返回一个 JSON,其中字段 user.username 是该用户的用户名。安装了curl和jq后,这个shell oneliner将直接返回用户名:

curl -s https://i.instagram.com/api/v1/users/USERID/info/ | jq -r .user.username
Run Code Online (Sandbox Code Playgroud)

或者,Instaloader 包提供了一种便捷的方法来获取给定 ID 的用户名,而无需使用官方 Instagram API。

import instaloader

L = instaloader.Instaloader()
profile = instaloader.Profile.from_id(L.context, ID)
print(profile.username)
Run Code Online (Sandbox Code Playgroud)

它的工作原理是查询个人资料的帖子之一(仅需要个人资料的 ID),获取该帖子的元数据 (instagram.com/p/SHORTCODE),然后返回owner.username该帖子的值。