0

i've been working in this simple project that tells if the instagram user is already taken or still available by just searching into the source of the get requests

Anyway when I implemented the script with manually user input it worked totally fine and here's the code :

    import requests

def insta_check():
    i = 10
    while i > 0:
        print()
        username = input(" Input user to check or Enter (q) 2 quit : ").lower()

        if username == 'q':
            print()
            print(" Good Bye ^_^ ")
            break
        print()
        url = 'https://www.instagram.com/' + username

        x = requests.get(url)
        y = x.text

        if 'Page Not Found' in y:
            print("[+] The user [ {} ] is Available or bieng disabled by the user owner :) ".format(username))
        elif 'Page Not Found' not in y:
            print("[+] The user [ {} ] is Not Available :( ".format(username))
        print()
insta_check()

but when I tried to get the inputs from an output file the get request start giving me wrong results ( it shows that all users are available ) IDK why and that's what I'm asking about

    import requests

f = open("users", "r")

def insta_checker():


    for username in f:


        url = 'https://www.instagram.com/' + username
        x = requests.get(url)
        y = x.text

        if 'Page Not Found' in y:
            print("[+] The user [ {} ] is Available :) ".format(username))
        elif 'Page Not Found' not in y:
            print("[+] The user [ {} ] is Not Available :( ".format(username))
        print()


insta_checker()
4
  • Are you sure that your script isn't simply blocked because instagram doesn't want to be crawled? Commented May 9, 2020 at 18:51
  • I ran the same thing and it worked for me. Maybe instagram blocked you? Try printing the output of the get request. Commented May 9, 2020 at 18:52
  • you didn't read() from the file at any point Commented May 9, 2020 at 18:54
  • I don't think that I'm blocked because it worked but with wrong results, and for the reading I read all the users from the file and if u wanna check just print(url) and u will see that it's getting the inputs normally Commented May 9, 2020 at 19:11

1 Answer 1

1

When you do for username in f it reads the whole line including the \n character at the end and appends that to the url which is why it is showing username is available. You need to strip() the username to remove any whitespace (or rstrip() to strip only from the right).

Additionally, it is better to use with open('file', 'r') as f: to auto close the file when done, for example:

import requests

def insta_checker():
    with open('users', 'r') as f:
        for username in f:
            username = username.rstrip() # remove newline character
            url = 'https://www.instagram.com/' + username
            x = requests.get(url)
            y = x.text
            if 'Page Not Found' in y:
                print("[+] The user [ {} ] is Available :) ".format(username))
            elif 'Page Not Found' not in y:
                print("[+] The user [ {} ] is Not Available :( ".format(username))
            print()

insta_checker()

Alternatively, you could use f.read().splitlines() to get rid of trailing newline character for each line only:

with open('users', 'r') as f:
    for username in f.read().splitlines():
        # rest of code
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.