2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでGoogle翻訳APIを使ってみる

Posted at

下記の手順で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)

参考サイト

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?