|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我看了python第54课后开始也爬一下有道翻译,但是出现了一些错误百思不得其解,我的代码如下:
import urllib.request
import urllib.parse
import json
content = input("请输入需要翻译的内容:")
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc'
data = {
"type" : "AUTO",
"i" : content,
"doctype" : "json",
"xmlVersion" : "1.8",
"keyfrom" : "fanyi.web",
"ue" : "UTF-8",
"action" : "FY_BY_CLICKBUTTON",
"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)
然后出现的错误是:Traceback (most recent call last):
File "C:\Users\z\Desktop\111.py", line 22, in <module>
response = urllib.request.urlopen(url,data,head)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 543, in _open
'_open', req)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1016, in _send_output
self.send(msg)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 956, in send
self.connect()
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Users\z\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 713, in create_connection
sock.settimeout(timeout)
TypeError: an integer is required (got type dict)
大神们求指点!!!
|
|