【问题标题】:Is there any way of getting instagram User _ID without using the instagram API?有什么方法可以在不使用 instagram API 的情况下获取 instagram User _ID?
【发布时间】:2016-04-20 04:44:08
【问题描述】:

我的任务不是生成任何应用程序或网站,而只是编写一个 PYTHON 脚本,该脚本将用户名作为输入并检索用户 ID 作为结果。

由于 instagram 要求提供应用程序名称、网站、URI 和所有其他能够接收客户端 ID 和其他能够使用其 API 的东西,而我没有这样的应用程序,我没有有资格获得它。 那么有什么方法可以在没有 Instagram API 的情况下做到这一点?

如果没有其他办法,怎么办?

我也是 python 编程和连接 API 的新手。如果有人能帮助我编写代码,那将非常有帮助。

谢谢!

【问题讨论】:

  • 如果有官方API,最好坚持使用。您应该能够在没有网站的情况下注册 API。
  • 感谢@Selcuk 的回复。但是我检查了它并要求所有这些。由于我是使用 API 概念的 Python 新手,所以我不知道如何编写相同的代码。请帮助如何做到这一点。
  • 好的,让我尝试做占位符,是的,我从github.com/facebookarchive/python-instagram 得到了 python-instagram api 的东西。我安装了它。我不知道如何使用这些东西。代码将如何使用它来简单地获取用户 ID
  • @Selcuk 嘿!正如你所说,我使用占位符注册了 Instagram。你能帮我写一个如何使用它的小代码吗?
  • @Selcuk 没问题,伙计!我明白了。

标签: python python-3.x instagram instagram-api


【解决方案1】:

这个问题的答案是我们可以使用 Instagram API 本身。正如@Selcuk 所说,我们可以在 Instagram 网站上使用占位符进行注册。因此,我们将从中获取 client_id 和 client_secret,如果我们是注册 instagram 用户,我们也会获得访问令牌。

访问令牌如果很难找到,可以通过https://apigee.com/console/instagram获得 然后在顶部的身份验证选项中,选择 OAuth 这将带您进入 instagram 的登录页面。发送查询后,它会显示您提出的请求,并且还可以找到访问令牌。

在 python 控制台中输入:-

import urllib.request
import urllib.parse
 from instagram.client import InstagramAPI
access_token="YOUR ACCESS TOKEN&q"
api=InstagramAPI(client_id= 'YOUR CLIENT ID', client_secret='YOUR CLIENT SECRET')
scope='public_content'
url='https://api.instagram.com/v1/users/search?q=USERNAME TO SEARCH&access_token=YOUR ACCESS TOKEN'
a=urllib.request.urlopen(url)
print(a.read())

这将为您提供有关用户的详细信息。

【讨论】:

  • 这只会给你访问令牌的用户名,这对其他用户没有帮助。
【解决方案2】:

我把这段代码放在下面(python 3.5.2)只使用urllib,它就像一个梦!

from six.moves.urllib.request import urlopen

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
id_loop = True
while id_loop == True:
    username = input('Please enter the Instagram username that you would like to find the User ID for: ') 

    link = 'http://www.instagram.com/' + username
    response = urlopen(link)
    content = str(response.read());

    start_index = (content.index('"owner": {"id": "')) + len('"owner": {"id": "')

    test_string = ''

    for collect in range(14):
        test_string = test_string + content[start_index]
        start_index = start_index + 1

    edit_string = ''

    for char in test_string:
        if is_number(char) == True:
            edit_string = edit_string + char
        else:
            edit_string = edit_string + ''

    print(username + "'s Instagram ID = " + edit_string)

如您所见,您需要的唯一模块是 urllib。此代码实质上是查找您输入的用户名的 Instagram 页面,并将变量 content 设置为等于由网页的整个 html 代码组成的单个字符串。然后通过代码搜索此字符串以查找用户 Instagram ID。希望这会有所帮助!

【讨论】:

    【解决方案3】:

    猜测代码更改中的事情,但生活中的挑战仍然存在...... 我们开始吧......一些简洁的python代码让事情变得更容易!

    python函数用户名到id

    from six.moves.urllib.request import urlopen
    
    
    def get_insta_id(username):
        link = 'http://www.instagram.com/' + username
        response = urlopen(link)
        content = str(response.read())
        start_pos = content.find('"owner":{"id":"') + len('"owner":{"id":"')
        end_pos = content[start_pos:].find('"')
        insta_id = content[start_pos:start_pos+end_pos]
        return insta_id
    
    
    print(get_insta_id("username"))
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多