import time,random,json
from func_all import post
#md5加密函数
def md5(need_str):
import hashlib
# 创建md5对象
md5_o = hashlib.md5()
# 需要有bytes, 作为参数
sign_bytes = need_str.encode('utf-8')
# print(type(sign_bytes))
# 更新md5 object的值
md5_o.update(sign_bytes)
sign_str = md5_o.hexdigest()
return sign_str
def translate(kw):
#url
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': 'OUTFOX_SEARCH_USER_ID=298855370@10.169.0.83; OUTFOX_SEARCH_USER_ID_NCOO=854769824.938877; fanyi-ad-id=47865; fanyi-ad-closed=1; JSESSIONID=aaakqE9hTCGkigY9vw2uw; ___rl__test__cookies=1534216152192',
'Host': 'fanyi.youdao.com',
'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
# form 的生成1. i 需要确定, 2, salt, 3, sign
key = kw
# salt : ((new Date).getTime() + parseInt(10 * Math.random(), 10))
salt = int(time.time()*1000 + random.randint(0,10))
salt_str = str(salt)
# sign : o = u.md5(S + n + r + D);
# S = "fanyideskweb"
# D = "ebSeFb%=XZ%T[KZ)c(sy!"
# n = key
# r = salt_str
S = "fanyideskweb"
D = "ebSeFb%=XZ%T[KZ)c(sy!"
sign_str = S + key + salt_str + D
# 调用md5加密
sign_str = md5(sign_str)
form = {
'i': key,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt_str,
'sign': sign_str,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTIME',
'typoResult': 'false',
}
html_bytes = post(url,form,headers=headers)
#将json类型的字符创转换为字典
res_dict = json.loads(html_bytes.decode('utf-8'))
translate_res = res_dict['translateResult'][0][0]['tgt']
return translate_res
if __name__ == '__main__':
kw = input('请输入要翻译的内容:')
ret = translate(kw=kw)
print(kw + '的翻译是:' + ret)
导入的模块func_all
from urllib import request,parse
from urllib.error import HTTPError,URLError
# get请求函数
def get(url,headers=None):
return urlrequest(url,headers=headers)
# post请求函数
def post(url,form,headers=None):
return urlrequest(url,form,headers=headers)
def urlrequest(url,form=None,headers=None):
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
if headers == None:
headers = {
"User-Agent": user_agent
}
html_byte = b''
# 异常处理
try:
if form:
# POST请求
# 转换成str
form_str = parse.urlencode(form)
# 转换成bytes
form_bytes = form_str.encode('utf-8')
req = request.Request(url, data=form_bytes, headers=headers)
else:
# get请求
req = request.Request(url, headers=headers)
# 发送请求
response = request.urlopen(req)
html_byte = response.read()
except HTTPError as e:
print(e)
except URLError as e:
print(e)
return html_byte
if __name__ == "__main__":
url = "http://fanyi.baidu.com/sug"
data = {
'kw': '呵呵'
}
html_byte = post(url,form=data)
print(html_byte)
# url = "http://www.baidu.com"
# html_byte = get(url)
# print(html_byte.decode('utf-8'))