Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to make a program that checks username availability on Ubisoft.com. The code I now have should be printing 'available' if the page says "Nothing is broken" and 'taken' if the username is already taken.

An example of a taken username is https://ubisoftconnect.com/en-US/profile/test

And an example of an available username is https://ubisoftconnect.com/en-US/profile/test1241924

Here's the code to it:
import requests

r = requests.get('https://ubisoftconnect.com/en-US/profile/test')
text = r.text

if "Nothing is broken" in text:
    print('available')
else:
    print('taken')


Now for some reason, the code will always output 'available', even if it's a taken username and therefore has no "Nothing is broken" in text.

Can someone help me work this out to only say 'available' if the username is indeed available and make it output 'taken' if the username is indeed taken

What I have tried:

I have tried to use other text inputs for if "txt" in text
Posted
Updated 7-May-21 1:55am

That is most likely because the returned text always contains the string "Nothing is broken". Examination of the data will confirm.
 
Share this answer
 
Comments
gxzs. 7-May-21 7:23am    
How do I fix that.
If I change the code to
if "Nothing is broken" in text:            print('available')   
if "Games owned" in text:             print('taken')


It puts out both available and taken, but how do I fix it
Richard MacCutchan 7-May-21 7:30am    
You are testing the wrong things. Both of these strings are in the text that is returned every time, so they are no use to you. You need to find the unique string that tells you whether the username is taken or not.
Dave Kreskowiak 7-May-21 11:38am    
You are STILL making assumption about what your request is getting back from the server. You are not going to get just a small string that says what you are testing for. You are getting back the HTML for entire web pages.

That's why I said check with Ubisoft to see if there's an API you can use.

What you're trying to do is web scraping, and that is a pain in the ass when the vendor decides to change their web page.
gxzs. 7-May-21 7:37am    
I have gotten no clue how to do that, for me the /test and the /test1241924 are completely different websites with different texts
Richard MacCutchan 7-May-21 7:50am    
Well you need to talk to the owners of the websites. There is no way anyone here can tell you what the data is supposed to contain.
Quote:
How do I fix that.

The only way to do that is to use the debugger (pdb — The Python Debugger — Python 3.9.5 documentation[^]) to look at exactly what you have returned from the site, and check exactly what is happening to that data as your code runs.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900