I have a dictionary which looks like this:
websites = {
"instagram": f'https://www.instagram.com/{username}',
"facebook": f'https://www.facebook.com/{username}'
}
And a For loop for iterating over keys of the dictionary
for key in websites.keys():
keys = str(key)
# print(keys)
if keys == target:
# print(target)
# print(keys)
search_web(username, websites[key])
break
My search_web function:
def search_web(username, tgt):
print("search_web is running")
r = requests.get(tgt)
print(tgt)
if r.status_code == 200:
print('Got it ' + username + ' in ' + tgt)
elif r.status_code == 400:
print('Not Found in ' + username + ' at ' + tgt)
This is what the Output looks like:
python search_user.py -u vybhav_72954 -t instagram github
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Now I understand what's going on, The Instagram loop is running 9 times, because of 9 literal characters and similarly, Github is running because of 6 literal characters.
I understand that the break statement is used to break out of If loop, once the condition is met, but in this case, rather than breaking the condition we are looping again. I tried changing the position of break and my function call, but still the problem persists, am sure this is a basic mistake, can someone guide me here.
Expected Output:
Got it vybhav_72954 in https://www.instagram.com/vybhav_72954
Not Found vybhav_72954 at https://www.github.com/vybhav_72954
Just in case if it's needed, the argparse function:
def parser_input():
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--usernames", help="Enter the username.", type=str, required=True)
parser.add_argument("-t", "--targets", help="Enter the website(s).", type=str, required=True, nargs='+')
if len(sys.argv) == 1:
parser.print_help()
sys.exit()
return parser.parse_args()
My main function also has a for loop, which triggers inputs function which contains the for loop, dictionary and the function call to search_web function.
if __name__ == '__main__':
arg = parser_input()
# listToStr = ' '.join([str(elem) for elem in arg.targets])
for i in range(len(arg.targets)):
for l in arg.targets[i]:
inputs(arg.usernames, arg.targets[i])
I have used the for loop to run the code separately on the strings contained in list generated by arg.target [as it has nargs='+']
Aucun commentaire:
Enregistrer un commentaire