繁体   English   中英

在 Python 脚本中使用 API microsoft 翻译器

[英]Using API microsoft translator in a Python script

我正在用 Python 编写一个脚本来检测所提供文本的语言。

我发现以下命令行可以在终端中使用,但我想在我的脚本中使用它。

命令 :

**curl -X POST "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Content-Type: application/json" -d "[{'Text':'What language is this text written in?'}]"**.

在脚本中,像客户端机密、“ text ”等元素……应该在变量中。 我想在一个变量中捕获整个命令行的结果,然后将其打印给用户。

我怎样才能做到这一点?

我在这里找到了命令行。

命令行中的Command Line本质上是发送http request 所以你只需要使用我下面提供的python代码,仅供参考。

import requests
import json

url = 'https://api.cognitive.microsofttranslator.com//Detect?api-version=3.0'
body =[{"text": "你好"}]
headers = {'Content-Type': 'application/json',"Ocp-apim-subscription-key": "b12776c*****14f5","Ocp-apim-subscription-region": "koreacentral"}
r = requests.post(url, data=json.dumps(body), headers=headers)
result=json.loads(r.text)
a=result[0]["language"]
print(r.text)
print("Language = " + a)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM