关于《零基础入门学习python》 第 54讲的关于有道翻译的问题
翻译代码如下:import urllib.request
import urllib.parse
url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data = {}
data["i"] = "i love you"
data["from"] = "AUTO"
data["to"] = "AUTO"
data["smartresult"] = "dict"
data["client"]= "fanyideskweb"
data["salt"] = "16231401493077"
data["sign"]= "97db933e50154143fc987144a4e20369"
data["lts"] = "1623140149307"
data["bv"] = "8d7eb197ef27eea128b1736965d36a09"
data["doctype"] = "json"
data["version"] = "2.1"
data["keyfrom"]= "fanyi.web"
data["action"] = "FY_BY_CLICKBUTTION"
data = urllib.parse.urlencode(data).encode("utf-8")
response = urllib.request.urlopen(url,data)
html = response.read().decode("utf-8")
print(html)
输出结果:
{"errorCode":50}
而小甲鱼视频中应该输出类似如下的字典:
{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"i love you","tgt":"我爱你"}]]}
而在翻阅论坛其他小伙伴的翻译代码时,发现只要将源代码中的 url 的地址改为"http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult"时就可正确输出:
{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"i love you","tgt":"我爱你"}]]}
可是网页上的url请求就如源代码中的一样:
请求 URL: https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule
请求方法: POST
状态代码: 200 OK
远程地址: 220.181.76.250:443
引用站点策略: strict-origin-when-cross-origin
求大佬讲解为什么会这样,球球了!!! {:10_266:}{:10_266:}{:10_266:}
import urllib.request
import urllib.parse
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = {}
data["i"] = "i love you"
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1538295833420'
data['sign'] = '07'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTIME'
data['typoResult'] = 'false'
data = urllib.parse.urlencode(data).encode("utf-8")
response = urllib.request.urlopen(url,data)
html = response.read().decode("utf-8")
print(html)
改这样试试,好像是有道的规则变了,我也不是很清楚, 我也最近才学,当时我也遇到你这样的问题,最后百度了一下解决了 ai153508847 发表于 2021-6-9 06:09
import urllib.request
import urllib.parse
那你的那个url请求的地址去哪找的{:10_266:} 口可口可22 发表于 2021-6-10 16:49
那你的那个url请求的地址去哪找的
url 中的 _o 去掉就行,_o是有道给的假 url ,好久以及就是这样了
参考代码:
import urllib.request
import urllib.parse
url = "https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = {}
data["i"] = "i love you"
data["from"] = "AUTO"
data["to"] = "AUTO"
data["smartresult"] = "dict"
data["client"]= "fanyideskweb"
data["salt"] = "16231401493077"
data["sign"]= "97db933e50154143fc987144a4e20369"
data["lts"] = "1623140149307"
data["bv"] = "8d7eb197ef27eea128b1736965d36a09"
data["doctype"] = "json"
data["version"] = "2.1"
data["keyfrom"]= "fanyi.web"
data["action"] = "FY_BY_CLICKBUTTION"
data = urllib.parse.urlencode(data).encode("utf-8")
response = urllib.request.urlopen(url,data)
html = response.read().decode("utf-8")
print(html)
Twilight6 发表于 2021-6-15 13:00
url 中的 _o 去掉就行,_o是有道给的假 url ,好久以及就是这样了
参考代码:
好的,比心心{:10_266:}
页:
[1]