我正在尝试使用urllib.request.urlopen()进行POST以及我的工作完美,除非数据中有双引号。我在调用urlopen()之前使用urllib.parse.urlencode()和bytes()。我还需要做什么?这是功能:
def getHypotheses(server, lang, dym, query):
param = urllib.parse.urlencode({"query": query, "count": "10"})
param_data = bytes(param,"utf-8")
url = "%s/aqs/%s/%s" % (server, lang, dym)
print ('query = ', query)
try:
connection = urllib.request.urlopen(url, param_data)
except urllib.error.HTTPError as err:
print ('param = ', param)
print ('url = ', url, ', query = ', query, ',
param_data = ', param_data)
sys.exit()
response = connection.read().decode('utf-8')
jsonobj = json.loads(response)
return jsonobj
和输出样本:
query = IHBK
query = B;NS
query = ASI
query = MDN
query = "CMANI
param = count=10&query=%22CMANI
url = http://localhost:8080/myserver/aqs/foo/bar ,
query = "CMANI , param_data = b'count=10&query=%22CMANI'
有什么想法吗?谢谢! - 戴夫