github上的代码稍微改了改
1 import urllib.request 2 import urllib.parse 3 import json 4 import os 5 6 7 while (1): 8 url = r‘http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/‘ 9 10 ‘‘‘ 11 head={} 12 head[‘User-Agent‘] = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36‘ 13 ‘‘‘ 14 content = input("input the next original text : ") 15 data={} 16 17 data[‘type‘] = ‘AUTO‘ 18 data[‘i‘] = content 19 data[‘doctype‘] = ‘json‘ 20 data[‘xmlVersion‘] = ‘1.6‘ 21 data[‘keyfrom‘] = ‘fanyi.web‘ 22 data[‘ue‘] = ‘UTF-8‘ 23 data[‘typoResult‘] = ‘true‘ 24 25 data = urllib.parse.urlencode(data).encode(‘utf-8‘) 26 27 reponse = urllib.request.urlopen(url,data) 28 html = reponse.read().decode(‘utf-8‘) 29 30 target = json.loads(html) 31 os.system(‘cls‘) 32 print("original text: %s"%content) 33 print("translation : %s"%(target[‘translateResult‘][0][0][‘tgt‘])) 34 print(‘\n‘) 35 print(‘\n‘) 36 37
不知道这个人是不是原创
链接 http://www.cnblogs.com/90zeng/p/python_translation.html?utm_source=tuicool&utm_medium=referral
然后 改成requests
1 import requests 2 import json 3 import os 4 5 6 while (1): 7 url = r‘http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/‘ 8 9 ‘‘‘ 10 head={} 11 head[‘User-Agent‘] = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36‘ 12 ‘‘‘ 13 content = input("input the next original text : ") 14 data={} 15 16 data[‘type‘] = ‘AUTO‘ 17 data[‘i‘] = content 18 data[‘doctype‘] = ‘json‘ 19 data[‘xmlVersion‘] = ‘1.6‘ 20 data[‘keyfrom‘] = ‘fanyi.web‘ 21 data[‘ue‘] = ‘UTF-8‘ 22 data[‘typoResult‘] = ‘true‘ 23 24 25 reponse = requests.get(url,params=data) 26 html = reponse.content.decode(‘utf-8‘) 27 28 29 target = json.loads(html) 30 os.system(‘cls‘) 31 print("original text: %s"%content) 32 print("translation : %s"%(targe[‘smartResult‘][‘entries‘][1:])) 33 print(‘\n‘) 34 print(‘\n‘)
再后来 申请了有道API 用API真方便QAQ
1 import requests 2 import json 3 import os 4 5 6 7 url = r‘http://fanyi.youdao.com/openapi.do?keyfrom=*******&key=*******&type=data&doctype=json&version=1.1&q=‘ 8 9 10 otext = input("input the next original text : ") 11 print(‘\n‘) 12 13 14 reponse = requests.get(url+otext) 15 html = reponse.content.decode(‘utf-8‘) 16 target = json.loads(html) 17 18 19 j=0 20 if(target.get("basic",‘not found‘)!=‘not found‘): 21 print(phonetic : "+‘[‘+target["basic"]["phonetic"]+‘]‘) 22 print(‘\n‘) 23 for i in target["basic"]["explains"]: 24 if (i): 25 print(target["basic"]["explains"][j]) 26 j=j+1 27 else: 28 print(target["translation"])
时间: 2024-10-11 20:30:46