0

I am new to python

Here is my attempt

import urllib2
email_id = input("enter the email id")
content = urllib2.urlopen('https://somewebsite.com/uniqueEmail?email={email_id}.format(email_id=email_id)').read()
print content
if content.find('"uniqueEmail":false') == True:
    print("email exists")
else:
    print("email doesnt exists")

when the print content code executes my website will display

{"params":{"method":"GET","controller":"GlobalUsers","action":"uniqueEmail","email":"theemailaddress_entered"},"uniqueEmail":true}

so if uniqueEmail prints out true or false depending upon if the email exists.

Now my doubt is

I know I did wrong in passing the email id variable through the parameter in the URL.

and second is how to check if the value was true or false ?

1
  • 1
    find() doesn't return True or False. It returns a number. Look at the documentation. What's more, this is JSON, which you can parse. Commented Aug 23, 2015 at 17:01

1 Answer 1

4

Yes, you did wrong in passing the email-id variable, the .format() should be outisde the string, not inside it. Example -

content = urllib2.urlopen('https://somewebsite.com/uniqueEmail?email={email_id}'.format(email_id=email_id)).read()

Secondly, if your website returns a response like -

{"params":{"method":"GET","controller":"GlobalUsers","action":"uniqueEmail","email":"theemailaddress_entered"},"uniqueEmail":true}

This seems like a json response, so you should parse it as json, using the json module and then that would return you a dictionary, and from the dictionary you can get the uniqueEmail information. Example -

content = urllib2.urlopen('https://somewebsite.com/uniqueEmail?email={email_id}'.format(email_id=email_id)).read()

import json
contentdict = json.loads(content)
if contentdict.get('uniqueEmail':   
    print("email exists")
else:
    print("email doesnt exists")

I am using dict.get() as that returns None if the key is not present, in which case it would mean the email doesn't exist.


Also, since you are using Python 2.7 as can be seen from the comment -

any idea why this error is being thrown now Traceback (most recent call last): File "C:/Python27/programs/email.py", line 2, in <module> email_id = input("enter email id ") File "<string>", line 1, in <module> NameError: name 'test' is not defined

You should use raw_input() instead of input() . Example -

email_id = raw_input("enter the email id")
Sign up to request clarification or add additional context in comments.

3 Comments

any idea why this error is being thrown now Traceback (most recent call last): File "C:/Python27/programs/email.py", line 2, in <module> email_id = input("enter email id ") File "<string>", line 1, in <module> NameError: name 'test' is not defined
You are using python 2.7 , use raw_input . Also you should include the version in the tags in your question.
Glad I could be helpful! :)

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.