URI :https://fanyi.baidu.com/transapi
使用方法:
import requests
def trans(query):
headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"}
data = """
from:zh
to:en
query:%s
"""%query
data ={key:value for key,value in [i.split(":") for i in data.split()]}
url = 'https://fanyi.baidu.com/transapi'
r = requests.post(url,data=data,headers=headers)
target = r.json()["data"][0]["dst"]
return target
def main():
while True:
query = input("请输入待翻译的字词:")
if query == "":break
else:
print(trans(query))
if __name__ == "__main__":
main()
结果:
请输入带翻译的字词:生活不止眼前的苟且
Life is more than the present.
请输入带翻译的字词:还有诗和远方
And poetry and distance
请输入带翻译的字词: