简体   繁体   中英

Is there any way of getting instagram User _ID without using the instagram API?

My task is NOT to generate any application or website but just to write a PYTHON script which gives usernames as an input and retrieve User ID as a result.

As instagram asks for Application name, website, URI and all other stuff to be able to receive a client ID and other things to be able to use their API and I don't have an application as such, I don't qualify for it. So is there any way we can do it without the Instagram API?

If there is no other way, how to work for it?

Also I am new to python programming and connecting with API and all. It would be really helpful if someone would help me with a code to do so.

Thanks!

Well the answer to the question is we can use the Instagram API itself. As @Selcuk said, we can register using placeholders in the Instagram site. So then we will get the client_id and client_secret from it and also if we are registered instagram user, we will get the access token too.

The access token if hard to find, can be achieved by going to https://apigee.com/console/instagram and then in the authentication option on the top, select OAuth which will take you to the login page of instagram. After sending the query it will show you what request you made and can find the access token too.

In the python console just type:-

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

This will give you the details about the user.

I put this code together below (python 3.5.2) only using urllib and it works like a dream!

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)

As you can see, the only module that you need is urllib. This code essentially finds the Instagram page of the username that you input and sets the variable content equal to a single string consisting of the entire html code of the webpage. This string is then searched by the code to find the users Instagram ID. Hope this helps!

Guess things in code change but life's challenges stay... here we go.... some neat python code to make things easier!

python function username to 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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM