Created
December 31, 2017 06:54
-
-
Save redstoneleo/56a153f97e0ce37f4170b662de3f7b97 to your computer and use it in GitHub Desktop.
invalid baiduTranslate----
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def baiduTranslate(self, queryText): | |
queryUrl = 'http://fanyi.baidu.com/v2transapi' | |
response = requests.post(queryUrl, data={'from': 'en', 'to': 'zh', 'query': queryText, 'transtype': 'realtime', 'simple_means_flag': 3,'sign':'498118.235959','token':'40a25d2a0d533f7cc897a454a0a86812'}) | |
repliedJson = response.json() | |
print(repliedJson) | |
dictResult = repliedJson["dict_result"] | |
if not dictResult: # 句子的时候字典没结果,teaching posts也没有结果 | |
print('baiduTranslate 翻译', queryText) # , repliedJson["trans_result"]["data"][0]["dst"] | |
return repliedJson["trans_result"]["data"][0]["dst"] | |
simpleMeans = dictResult["simple_means"] | |
result = '{}\n\n'.format(simpleMeans["word_name"]) | |
# print(simpleMeans["word_name"]) # response.history, | |
# print(simpleMeans["symbols"][0]['parts']) | |
for part in simpleMeans["symbols"][0]['parts']: # 单词释义,可以包含空格' good ' | |
# print(part) | |
result += '{}{}\n'.format(part.get("part") or '', ','.join(part["means"])) | |
# print(part.get("part"), ','.join(part["means"])) # queryText = 'private correspondence'的时候无'part',有'part_name' | |
print('baiduTranslate 字典', queryText) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment