Created
March 2, 2020 01:43
-
-
Save rovesoul/d4f2f3b7b5379a76ce0a249ee6127844 to your computer and use it in GitHub Desktop.
This file contains 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
#!/Users/donghuibiao/anaconda3/bin/python3.7 | |
# # -*- coding:utf-8 -*- | |
import json , requests, random | |
import time | |
session = requests.session() | |
# 仅翻译单词的 | |
def trans(word): | |
# 网址和表单 | |
url = 'https://fanyi.baidu.com/sug' | |
# 请求头 | |
i=str(random.randint(0,14)) | |
j=str(random.randint(0,14)) | |
n=str(random.randint(0,14)) | |
headers = {'User-Agent':f'Mozilla/{i}.0 (Macintosh; Intel Mac OS X '+i+'_'+j+'_'+n +')' | |
'AppleWebKit '+n+j+i+'.36 (KHTML, like Gecko) Chrome', | |
'Accept': 'text/html,application/xhtml+xml,application/xml;' | |
'q=0.9,image/webp,*/*;q=0.8'} | |
# post方式请求网站 | |
Form_data = {'kw': word} | |
response = requests.post(url, data=Form_data,headers=headers) | |
content = json.loads(response.text) # 加载成字典形式 | |
try: | |
result = content['data'][0]['v'] # 获取翻译结果 | |
return result | |
except Exception as e: | |
return 'NULL' | |
def get_trans(word): | |
word=word | |
a=trans(word) | |
if a!='NULL': | |
return a | |
else :return '抱歉,没查到!' | |
# | |
while 1: | |
try: | |
word = input('input your puzzle word:') | |
if word != 'q': | |
print(word,':\n',get_trans(word)+'\n') | |
time.sleep(0.5) | |
else: | |
print('\n>>>你按了退出,goodbye<<<') | |
break | |
except Exception as e: | |
print('\n输入错误,继续,或按q退出\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment