下記の手順でGoogle翻訳APIを有効に
- Google APIの申請
- プロジェクトの作成
- 課金機能の有効化
- APIの有効化
- 認証の設定:APIキー
PythonでGoogle翻訳APIを使ってみる
from importlib import reload
import requests
import csv
import sys
# https://www.eyoucms.com/news/ziliao/other/29445.html
def translate_text(target, content):
language_type = ""
url = "https://translation.googleapis.com/language/translate/v2"
data = {
'key':"A×××c", # 認証の設定:APIキー
'source': language_type,
'target': target,
'q': content,
'format': "text"
}
#headers = {'X-HTTP-Method-Override': 'GET'}
#response = requests.post(url, data=data, headers=headers)
response = requests.post(url, data)
# print(response.json())
print(response)
res = response.json()
print(res["data"]["translations"][0]["translatedText"])
result = res["data"]["translations"][0]["translatedText"]
print(result)
return result
if __name__ == '__main__':
content = "begin"
target = 'zh-cn'
translate_text(target,content)
参考サイト