网址中语法错误无效的问题

时间:2014-08-06 00:04:45

标签: python variables url syntax pycurl

在我的代码中我试图让用户登录并检索一些信息,但是我的变量用户和密码出现语法错误。粗体打印在代码

中注释掉
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()

3 个答案:

答案 0 :(得分:0)

你必须通过加倍(或使用单引号)来转义字符串中的双引号字符:

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

但实际上它一定是这样的:

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)

此服务不希望您在uri中发送引号。但是特殊字符(比如'@')必须用'urllib.parse'类中的'quote'或'urlencode'方法进行url编码

答案 1 :(得分:0)

您需要在字符串中转义引号,或在外部使用单引号。

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

答案 2 :(得分:0)

不。重新开始。

import urllib.parse

 ...

qs = urllib.parse.urlencode((('email', user), ('password', password)))
url = urllib.parse.urlunparse(('https', 'www.eobot.com', 'api.aspx', '', qs, ''))
c.setopt(c.URL, url)