Search code examples
pythonvariablesurlsyntaxpycurl

Issues with invalid syntax error in a url


In my code Im trying to get the user to log in and retrive some information, but I get a syntax error with my variables user and password. bold print is commented out in code

import urllib.request
import time
import pycurl
#Log in
user = input('Please enter your EoBot.com email: ')
password = input('Please enter your password: ')
#gets user ID number
c = pycurl.Curl()
#Error below this line with "user" and "password"
c.setopt(c.URL, "https://www.eobot.com/api.aspx?email="user"&password="password")
c.perform()

Solution

  • You must escape double quotation character in string by doubling it(or by using single quotes also):

    c.setopt(c.URL, "https://www.eobot.com/api.aspx?email=""user""&password=""password""")
    

    but really it must be like this:

    from urllib import parse
    
    # ...
    # your code
    # ...
    
    url = 'https://www.eobot.com/api.aspx?email={}&password={}'.format(parse.quote(user), parse.quote(password))
    c.setopt(c.URL, url)
    

    This service don't want from you to send quotes in uri. But special characters(like '@') must be url-encoded by 'quote' or 'urlencode' methods from 'urllib.parse' class