有没有办法在不使用instagram API的情况下获取Instagram用户_ID?

时间:2016-04-20 04:44:08

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

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

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

如果没有其他办法,如何为它工作?

我也是python编程的新手,并且连接API和所有。如果有人帮我提供代码,那将非常有用。

谢谢!

3 个答案:

答案 0 :(得分: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())

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

答案 1 :(得分:1)

我只使用urllib将此代码放在下面(python 3.5.2),它就像一个梦想!

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。希望这有帮助!

答案 2 :(得分:0)

猜测代码中的事情会发生变化,但是生活中的挑战依然存在... 我们走了。...一些简洁的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"))
相关问题