0

我正在尝试将代码从 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 代码时,我收到此错误:

控制台错误

4

0 回答 0