网上找了好久, 终于弄好了, 免费的谷歌翻译,直接上代码,不懂留言:
//翻译 app.get('/google', function (req, res, next) { var content = req.query.content; //请求查找TKK中的数值(可以当爬虫使用) superagent .get('https://translate.google.cn/?hl=en') .end(function (err, sres) { // callback /**设置响应头允许ajax跨域访问**/ res.setHeader("Access-Control-Allow-Origin", "*"); /*星号表示所有的异域请求都可以接受,*/ res.setHeader("Access-Control-Allow-Methods", "GET,POST"); // 常规的错误处理 if (err) { return next(err); } var str = sres.text; console.log(str); var str1 = str.split('TKK')[1].substring(20, 79);//得到TKK附近的字符串 var a = str1.match(/3d(S*);/)[1]; var b = str1.substring(15, 74).match(/3d(S*);/)[1]; var c = str1.substring(29, 74).match(/return (S*)+/)[1]; var tkd = c + '.' + ((a - 0) + (b - 0)); //得到TK的值 var myTk = tk(content, tkd); // console.log(a + " " + b + " " + c + " " + myTk); var url_google = ""; if (/^[a-zA-Z]*$/.test(content)) { console.log("英文" + content); url_google = "https://translate.google.cn/translate_a/single?client=t&sl=en&tl=zh-CN&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&pc=1&ssel=0&tsel=0&kc=2&tk=" + myTk + "&q=" + content; } else if (/^[u4e00-u9fa5]*$/.test(content)) { console.log("中文" + content); url_google = "https://translate.google.cn/translate_a/single?client=t&sl=zh-CN&tl=en&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=btn&ssel=0&tsel=0&kc=0&tk=" + myTk + "&q=" + encodeURI(content); } //得到翻译的内容 superagent .get(url_google) .end(function (err, sres) { // callback if (err) { return next(err); } res.send(sres.text); }); }); }) function b(a, b) { for (var d = 0; d < b.length - 2; d += 3) { var c = b.charAt(d + 2), c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c), c = "+" == b.charAt(d + 1) ? a >>> c : a << c; a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c } return a } function tk(a, tkd) { var tkk = tkd + ""; for (var e = tkk.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) { var c = a.charCodeAt(f); 128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128) } a = h; for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6"); a = b(a, "+-3^+b+-f"); a ^= Number(e[1]) || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return a.toString() + "." + (a ^ h) }
测试:
总结: 现在google 翻译都收费了,找免费的不容易啊,还好有一个网页版免费的, 通过使用爬虫, 爬取必要信息,收集数据.
superagent: 这个方法很好用, 爬虫慢慢搞!!!!!
使用这个方法, 能爬取大部分资源,是做爬虫的好帮手,解决了资源不足的问题,觉的内容好,使用多线程,百万级资源分分钟搞定!
使用被人的接口,怕被禁,网速不好会有延迟,影响体验.
自己做一个接口放网上,想怎么用就怎么用.就是这么任性.
不会弄的联系我,方式你懂的.......