import requests
import json
class Jinshan(object):
def __init__(self):
self.name = input('输入你要翻译的词组:')
self.url = 'http://fy.iciba.com/ajax.php?a=fy'
self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}
def send_respent(self, form_porm):
respent = requests.post(self.url, headers=self.headers, data=form_porm)
data = respent.content.decode()
return data
def filer_wort(self, data):
data_dick = json.loads(data)
try:
reat = data_dick['content']['out']
except:
reat = data_dick['content']['word_mean']
print(self.name+'的翻译为:', reat)
def run(self):
form_porm = {
'f': 'auto',
't': 'auto',
'w': self.name
}
data = self.send_respent(form_porm)
self.filer_wort(data)
if __name__ == '__main__':
Jinshan().run()
结果如下:
英译汉:
汉译英: