鱼C论坛

 找回密码
 立即注册
查看: 1175|回复: 4

[已解决]python有道翻译的问题

[复制链接]
发表于 2018-12-10 12:44:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)


大神们求指点!!!
最佳答案
2018-12-10 12:54:55
head不能这么传,识别成timeout了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-10 12:54:55 | 显示全部楼层    本楼为最佳答案   
head不能这么传,识别成timeout了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-10 13:01:07 | 显示全部楼层
塔利班 发表于 2018-12-10 12:54
head不能这么传,识别成timeout了

为什么会识别成time out呢,我也是按小甲鱼那样弄head
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-10 13:15:30 From FishC Mobile | 显示全部楼层
header?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-10 14:17:12 | 显示全部楼层
2587632842 发表于 2018-12-10 13:01
为什么会识别成time out呢,我也是按小甲鱼那样弄head

要么定制opener打开
要么用Request类里添加header
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-30 03:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表