|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
win 10
python 3.6.2
代码如下:
import urllib.request
import urllib.parse
import json
while 1:
content=str(input('请输入需要翻译的内容:'))
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
data={}
data['i']=content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1506486158432'
data['sign']='7c1484518eee3f7628e3c7e6e09e8893'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_REALTIME'
data['typoResult']='true'
data=urllib.parse.urlencode(data).encode('utf-8')
response=urllib.request.urlopen(url,data,head)
html=response.read().decode('utf-8')
target=json.loads(html)
trans=target['translateResult'][0][0]['tgt']
print('翻译结果:%s'%(trans))
运行后错误提示如下:
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================== RESTART: E:\Python3.6.2\实验程序\50.2有道翻译.py ==================
请输入需要翻译的内容:haha
Traceback (most recent call last):
File "E:\Python3.6.2\实验程序\50.2有道翻译.py", line 31, in <module>
response=urllib.request.urlopen(url,data,head)
File "E:\Python3.6.2\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "E:\Python3.6.2\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "E:\Python3.6.2\lib\urllib\request.py", line 544, in _open
'_open', req)
File "E:\Python3.6.2\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "E:\Python3.6.2\lib\urllib\request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "E:\Python3.6.2\lib\urllib\request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "E:\Python3.6.2\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "E:\Python3.6.2\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "E:\Python3.6.2\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "E:\Python3.6.2\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "E:\Python3.6.2\lib\http\client.py", line 964, in send
self.connect()
File "E:\Python3.6.2\lib\http\client.py", line 936, in connect
(self.host,self.port), self.timeout, self.source_address)
File "E:\Python3.6.2\lib\socket.py", line 710, in create_connection
sock.settimeout(timeout)
TypeError: an integer is required (got type dict)
请问问题出在哪里了?
|
-
|