n00b_cod3r
n00b_cod3r

Reputation: 1

Get request with Python loop

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

Upvotes: 0

Views: 190

Answers (1)

Mohd
Mohd

Reputation: 5613

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

Upvotes: 1

Related Questions