Reputation: 353
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 ?
Upvotes: 0
Views: 69
Reputation: 91009
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")
Upvotes: 4