|
发表于 2018-1-25 16:49:28
|
显示全部楼层
代码如下:
import urllib.request
import urllib.parse
import json
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Mobile Safari/537.36'
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc'
data = {}
data['i'] = 'I love Fish'
data['type'] = 'AUTO'
data['ue'] = 'UTF-8'
data['doctype'] = 'json'
data['xmlVersion'] = '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')
print(html)
报错信息如下,各位帮忙看看是怎么回事?
======================= RESTART: F:/py/translation.py =======================
Traceback (most recent call last):
File "F:/py/translation.py", line 19, in <module>
response = urllib.request.urlopen(url,data,head)
File "D:\Program Files\python3.6.3\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "D:\Program Files\python3.6.3\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "D:\Program Files\python3.6.3\lib\urllib\request.py", line 544, in _open
'_open', req)
File "D:\Program Files\python3.6.3\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "D:\Program Files\python3.6.3\lib\urllib\request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "D:\Program Files\python3.6.3\lib\urllib\request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "D:\Program Files\python3.6.3\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "D:\Program Files\python3.6.3\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "D:\Program Files\python3.6.3\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "D:\Program Files\python3.6.3\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "D:\Program Files\python3.6.3\lib\http\client.py", line 964, in send
self.connect()
File "D:\Program Files\python3.6.3\lib\http\client.py", line 936, in connect
(self.host,self.port), self.timeout, self.source_address)
File "D:\Program Files\python3.6.3\lib\socket.py", line 710, in create_connection
sock.settimeout(timeout)
TypeError: an integer is required (got type dict)
>>> |
|