木鱼加小鱼 发表于 2017-12-29 11:05:39

jerryxjr1220 发表于 2017-4-30 16:26
data['salt'] = '1493538230701'这个应该是个时间戳,
data['sign'] = '7b50be5ddda95bce3d4b7b9404647c ...

使用提供的第一种方法,返回的是{"type":"EN2ZH_CN","errorCode":30,"elapsedTime":0,"translateResult":[[{"src":"您的请求来源非法,商业用途使用请关注有道翻译API官方网站\u201C有道智云\u201D: http:\/\/ai.youdao.com","tgt":"您的请求来源非法,商业用途使用请关注有道翻译API官方网站\u201C有道智云\u201D: http:\/\/ai.youdao.com"}]]}

东海神皇 发表于 2018-1-11 10:45:00

1304539791 发表于 2017-7-15 19:26
只要把07.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=nu ...

可是我的去掉‘_o’,出现的是ValueError,这是怎么回事啊

石小龙 发表于 2018-1-20 14:41:02

1304539791 发表于 2017-7-15 19:26
只要把07.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=nu ...

大神啊,我终于搞定了

lymadnzlh 发表于 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)
>>>

数字城堡 发表于 2018-1-26 23:11:46

骚强么么哒 发表于 2017-9-3 00:54
真的是这个问题。这么发现的?????为什么和_o有关系???

666,果然是_o的问题

数字城堡 发表于 2018-1-26 23:52:32

有人知道为什么翻译短的词句没问题,翻译长的词句就只能翻译前面很少的部分?

lijiyh 发表于 2018-1-30 16:39:02

还是大神们认真啊

chinalove 发表于 2018-2-25 17:01:16

好像我也是这个问题。正在解决中

ADME 发表于 2018-6-5 10:11:30

jerryxjr1220 发表于 2017-4-30 16:26
data['salt'] = '1493538230701'这个应该是个时间戳,
data['sign'] = '7b50be5ddda95bce3d4b7b9404647c ...

请问URL和data是从哪里找到了,我也是这个问题,但是我的url和data也是从谷歌浏览器里找到的,你的data和ur是怎么找的呢

kuz00 发表于 2018-6-6 14:18:24

确实是_0的问题啊,去掉就行了,原因是为啥?

帅楠楠 发表于 2018-6-6 15:35:34

import urllib.request
import urllib.parse
import json


content = input("qingshuru:")
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc'

head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'

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')

req = urllib.request.Request(url,data,head)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')

target = json.loads(html)
target = target['translateResult']['tgt']

print(target)


第一次问,有什么不好请说出来,想问各位大神为什么运行后输入翻译内容总提示请求来源非法不懂!!

Py20180701 发表于 2018-7-17 18:08:51

我的也是这样

xixi_96 发表于 2018-7-25 10:55:51

明显有道已经屏蔽了

jennypark 发表于 2018-8-2 10:39:36

发现一个问题,只要翻译'oracle'这个词,就会返回"errorCode":40 ,搜索了一下应该是“不支持的语言类型”,即使指定了from和to,结果都是一样,怎么破?

import urllib.request
import urllib.parse
content = input("请输入需要翻译的内容:")

url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data={}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1533171892573'
data['sign'] = 'a61c434ce376dc40dfdd999bc2aaaded'
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')
print(data)
response = urllib.request.urlopen(url,data)
html = response.read().decode('UTF-8')
print(html)

汝鱼得水 发表于 2018-8-15 14:06:23

cjb2008508 发表于 2017-7-18 15:00
我也发现了。去掉_o 就可以了,小甲鱼老师视频讲解时候的URL有道是没有_o的,不知道是不是有道的一个模糊 ...

请问那个_o 具体代表什么呢? 为啥会自动生成?

小文子 发表于 2018-9-3 22:47:15

{:5_106:}

Tristan66 发表于 2018-9-28 16:40:21

为什么和_o有关系???

静静静H 发表于 2018-11-19 23:10:48

1304539791 发表于 2017-7-15 19:26
只要把07.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=nu ...

十分感谢您!为什么这么神奇,这个纠结了我一个晚上,百度各种方法改了又改,原来是这个小小的_o。只是我不明白为什么这个改鸭

Heather_zero 发表于 2019-1-3 10:18:06

所有答案看了下了试了一下,果然需要修改的只是去掉request_html的‘_o’就解决问题了,不过原理就不是很清楚了,第一次贴个代码吧import urllib.request
import urllib.parse
import easygui asgui
import json

url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['i'] = 'I love FishC'
data['doctype'] = 'json'
data['Version'] = '2.1'
data['client'] = 'fanyideskweb'
data['keyform'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['action'] = 'FY_BY_CLICKBUTTON'
data['typoResult'] = 'true'
#print(data)

while True:
    sentense = gui.enterbox('请输入待翻译内容:', '盗版有道词典')
    data['i'] = sentense
    data = urllib.parse.urlencode(data).encode('utf-8')
    response = urllib.request.urlopen(url, data)
    html = response.read().decode('utf-8')
    #print(html)
    target = json.loads(html)
    gui.msgbox("%s的翻译结果是:\n\n %s" % (sentense, target['translateResult']['tgt']),\
               title = '盗版有道词典')
    if gui.ynbox('是否继续翻译:') == False :
      break

雪语风 发表于 2019-6-10 15:52:07

1304539791 发表于 2017-7-15 19:26
只要把07.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=nu ...

详细的解答可以参考https://blog.csdn.net/li939891142/article/details/84592442
页: 1 2 [3] 4
查看完整版本: python爬虫代码问题