有道有反爬机制,像salt,sign,lts都是有加密机制的,不能乱填,你需要去还原有道的加密机制
你还有加上请求头
urllib版的参考代码import urllib.request
import urllib.parse
import hashlib
import random
import time
import sys
url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
'Referer': 'https://fanyi.youdao.com/',
'Cookie': 'OUTFOX_SEARCH_USER_ID=1907954952@182.203.147.49; OUTFOX_SEARCH_USER_ID_NCOO=2032702153.1070416; fanyi-ad-id=306808; fanyi-ad-closed=1; ___rl__test__cookies=1654931146702'
}
lts = str(int(time.time()*1000))
salt = lts + str(random.randint(0,10))
sign_str = 'fanyideskweb' + "I love FishC!" + salt + 'Ygy_4c=r#e#4EX^NUGUc5'
m = hashlib.md5(sign_str.encode())
sign = m.hexdigest()
data = {
"i":"I love FishC!",
"from":"AUTO",
"to":"AUTO",
"smartresult":"dict",
"client":"fanyideskweb",
"salt":salt,
"sign":sign,
"lts":lts,
"bv":"a16a6033635b516a9006542112cdda8f",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_CLICKBUTTION"
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url,data,headers)
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
try:
resalut = eval(html)["translateResult"][0][0]["tgt"]
print("翻译结果:",resalut)
except:
print("翻译失败")
|