|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
翻译代码如下:
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
求大佬讲解为什么会这样,球球了!!!
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)
|
|