我正在尝试将代码从python 2转换为python 3,这是来自simsimi api的请求。
Python 2代码:
# -*- coding: utf- -*-
import urllib
import urllib2
url = "http://sandbox.api.simsimi.com/request.p?"
text_input = raw_input("Input your text: ")
lc_input = raw_input("Input your lc: ")
text = text_input.encode("utf-8")
lc = lc_input.encode("utf-8")
values = {'key' : 'api key',
'lc' : lc,
'ft' : '0.0',
'text' : text}
data = urllib.urlencode(values)
request_url = urllib2.Request(url, data)
response_post = urllib2.urlopen(request_url)
response = response_post.read().decode("utf-8")
print response
Python 3代码(结果):
# -*- coding: utf- -*-
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
url = "http://sandbox.api.simsimi.com/request.p?"
text_input = input("Input your text: ")
lc_input = input("Input your lc: ")
text = text_input.encode("utf-8")
lc = lc_input.encode("utf-8")
values = {'key' : 'api key',
'lc' : lc,
'ft' : '0.0',
'text' : text}
data = urllib.parse.urlencode(values)
request_url = urllib.request.Request(url, data)
response_post = urllib.request.urlopen(request_url).encode("utf-8")
response = response_post.read().decode("utf-8")
print(response)
当我运行python 3代码时,我收到此错误: