使用谷歌翻译 三种方式

1,谷歌官方发布 rest api
[url][u]http://code.google.com/intl/zh-CN/apis/language/translate/v2/getting_started.html[/u][/url]
使用rest
GET https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world

缺陷是 有一定的字节大小限制
【he URL for the GET, including parameters, must be less than 2K characters.

Note: You can also use POST to invoke the API if you want to send more data in a single request. The q parameter in the POST body must be less than 5K characters. To use POST, you must use the X-HTTP-Method-Override header to tell the Translate API to treat the request as a GET (use X-HTTP-Method-Override: GET)】
对于纯文本的 内容 可以截取分段翻译 对于html则可能截断标签 显示错误 当然可以写算法来避免 。
2.[url]
[u]http://code.google.com/p/google-api-translate-java/[/u][/url]
封装的一个

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class Main {
public static void main(String[] args) throws Exception {
// Set the HTTP referrer to your website address.
Translate.setHttpReferrer(/* Enter the URL of your site here */);

String translatedText = Translate.execute("Bonjour le monde",
Language.FRENCH, Language.ENGLISH);

System.out.println(translatedText);
}
}

3,自己模拟浏览器自己封装 优点是可以使用google没有公开的api
[url][u]http://translate.google.com.hk/translate?hl=zh-CN&ie=UTF-8&sl=en&tl=zh-CN&u=http://www.baidu.com/&prev=_t[/u][/url][align=center][/align]
缺点是谷歌防止爬取利用 进行的一些跳转加密 自己做的就是解密的过程
在此只是根据当时我们需要 只做了一个对网站url翻译的过程 且只针对以上网站
对应的url有三次变化
http://translate.google.com.hk/translate?hl=zh-CN&;
http://translate.google.com.hk/translate_p?hl=zh-CN&
http://translate.googleusercontent.com/translate_c?hl=zh-CN&
在这个过程中其中有个参数 usg有两次 变化
最后取得 翻译的内容
其中谷歌 比较霸道 url的传递顺序不同返回结果 不是你想要的结果
综合思路是 先用网页访问下 然后模拟着实验吧。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值