Python url中存在无效语法错误的问题
在我的代码中,我试图让用户登录并检索一些信息,但我的变量user和password出现语法错误。粗体打印在代码中被注释掉Python url中存在无效语法错误的问题,python,variables,url,syntax,pycurl,Python,Variables,Url,Syntax,Pycurl,在我的代码中,我试图让用户登录并检索一些信息,但我的变量user和password出现语法错误。粗体打印在代码中被注释掉 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.
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()
您必须通过使用单引号将itor加倍来转义字符串中的双引号字符:
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编码。您需要在字符串内部转义引号,或者在外部使用单引号
c.setopt(c.URL, 'https://www.eobot.com/api.aspx?email="user"&password="password"')
没有。重新开始
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)
我喜欢我的项目的这种方法,非常感谢。现在,如果我想用数字而不是文字,我还会用.still还是什么else@user3897196好对于数字url编码,实际上不需要它。这里没有需要编码的地方,所以您可以跳过它。