爬取国外网站的时候,总有一些单词要翻译,找了一些翻译接口,发现有道翻译API还可以,但是网上说有限制,一小时可以翻译1000次,但是实测可以抗住更多翻译的。如果有长篇要翻译,可以一次翻译一句话,或者多个词一起翻译。以实现大量翻译。
"""
有道翻译SDK
输入query_string
输出translated_string
"""
import requests
import json
import random
import re
class YouDaoTranslator(object):
def __init__(self, query_string):
self.url_temp = "http://fanyi.youdao.com/openapi.do?keyfrom={}&key={}&type=data&doctype=json&version=1.1&q={}"
self.query_string = query_string
def get_keyfrom_key(self):
"""每次翻译随机使用一组keyform&key"""
youdaoAPIs = [
{
"keyfrom": 'xujiangtao',
"key":'1490852988'
},
{
"keyfrom": 'ltxywp',
"key": '1092195550'
},
{
"keyfrom": 'txw1958',
"key": '876842050'
},
{
"keyfrom": 'youdanfanyi123',
"key": '1357452033'
},
{
"keyfrom": 'fadabvaa',
"key": '522071532'
},
{
"keyfrom": 'yyxiaozhan',
"key": '1696230822'
},
{
"keyfrom": 'siwuxie095-test',
"key": '2140200403'
},
{
"keyfrom": '11pegasus11',
"key": '273646050'
},
{
"keyfrom": 'webblog',
"key": '1223831798'
},
{
"keyfrom": 'wojiaozhh',
"key": '1770085291'
},
{
"keyfrom": 'atmoon',
"key": '1407453772'
},
{
"keyfrom": 'morninglight',
"key": '1612199890'
},
{
"keyfrom": 'Yanzhikai',
"key": '2032414398'
},
{
"keyfrom": 'JustForTestYouDao',
"key": '498375134'
},
{
"keyfrom": 'aaa123ddd',
"key": '336378893'
},
{
"keyfrom": 'aaa123ddd',
"key": '336378893'
},
{
"keyfrom": "wjy-test",
"key": "36384249"
},
{
"keyfrom": "youdao111",
"key": "60638690"
},
{
"keyfrom": "pythonfankjjkj1",
"key": "1288254626"
},
{
"keyfrom": "Dic-EVE",
"key": "975360059"
},
{
"keyfrom": "youdianbao",
"key": "1661829537"
},
{
"keyfrom": "AndroidHttpTest",
"key": "507293865"
},
{
"keyfrom": "123licheng",
"key": "1933182090"
},
{
"keyfrom": "pdblog",
"key": "993123434"
},
{
"keyfrom": "testorot",
"key": "1145972070"
},
{
"keyfrom": "node-translator",
"key": "2058911035"
},
{
"keyfrom": "mytranslator1234",
"key": "1501976072"
},
{
"keyfrom": "SkyHttpGetTest",
"key": "545323935"
},
{
"keyfrom": "htttpGetTest",
"key": "1230480132"
},
{
"keyfrom": "neverland",
"key": "969918857"
},
{
"keyfrom": "HTTP-TESTdddaa",
"key": "702271149"
},
{
"keyfrom": "fadabvaa",
"key": "522071532"
},
{
"keyfrom": "atmoon",
"key": "1407453772"
},
{
"keyfrom": "orchid",
"key": "1008797533"
},
{
"keyfrom": "chdego",
"key": "1347056326"
},
{
"keyfrom": "cxvsdffd33",
"key": "1310976914"
},
{
"keyfrom": "123licheng",
"key": "1933182090"
},
{
"keyfrom": "huichuang",
"key": "386196262"
},
{
"keyfrom": "eweewqeqweqwe",
"key": "957582233"
},
{
"keyfrom": "abc1243",
"key": "1207861310"
},
{
"keyfrom": "xxxxxxx",
"key": "1618693256"
},
{
"keyfrom": "mypydict",
"key": "27855339"
},
{
"keyfrom": "zqhong",
"key": "694644553"
},
{
"keyfrom": "wangtuizhijia",
"key": "1048394636"
},
{
"keyfrom": "xinlei",
"key": "759115437"
},
{
"keyfrom": "youdaoci",
"key": "694691143"
},
]
api_dict = random.choice(youdaoAPIs)
return api_dict
def get_api_url(self):
"""拼接翻译URL"""
api_dict = self.get_keyfrom_key()
url = self.url_temp.format(api_dict["keyfrom"],api_dict["key"],self.query_string)
return url
def parse(self,url):
"""解析翻译URL,返回翻译str"""
try:
resp = requests.get(url).text
resp_dict = json.loads(resp)
ret = resp_dict["translation"][0]
except Exception as e:
print("翻译报错",e)
ret = ""
return ret
def run(self):
"""启动翻译,返回被翻译的词汇"""
url = self.get_api_url()
translated_string = self.parse(url)
return translated_string
if __name__ == '__main__':
query_string = "book,dog,bird"
fanyi = YouDaoTranslator(query_string)
translated_string = fanyi.run()
print(translated_string)