请求谷歌翻译API,不过这个东西会随着谷歌的更新而改变,不知道今后是否可用。大家可以交流交流翻译API中TK参数的生成
#!/usr/bin/python
#coding: UTF-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import re
import urllib,urllib2 #引入接入网络接口API的模块
import sock_server
def translate(text,time):
values = {
'client':'t',
'sl':'en',
'tl':'zh-CN',
'hl':'zh-CN',
'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':'2',
'ssel':'0',
'tsel':'0',
'kc':'1',
'tk':time,
'q':text
}#暂时无用
values = {
'q':text
}
url='http://translate.google.cn/translate_a/single'
data = urllib.urlencode(values)
url="http://translate.google.cn/translate_a/t?client=webapp&sl=en&tl=zh-CN&hl=zh-CN&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=2&ssel=0&tsel=0&kc=1&tk="+time;#核心请求URL
req = urllib2.Request(url, data)
browser='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'#'验证头
req.add_header('User-Agent',browser)
response=urllib2.urlopen(req)
return response.read()
text=''' 10:46:20 Update Info: A new version of PyCharm is available! (show balloon)
Unregistered Vcs root detected
The directory F:\Repository\Gitar-Play is under Git, but is not registered in the Settings.
Add root Configure Ignore'''
tt=sock_server.get_tk(text)
print tt #计算url 请求中的一个参数
translate(text,tt)