0
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonからGoogleTranslateを無理やり使う

Last updated at Posted at 2017-05-26

モチベーション

英語論文翻訳をする際に本文をコピペするとpdfによっては変な改行や文字が入ることがある。
そのあたりをpythonで整形する際に一緒に翻訳までしちゃえば楽だと思ったのでその方法

方法

GoogleTranslateのapiは有料なので、seleniumでブラウザをエミュレートして、ブラウザのGoogleTranslateを使う

準備

pip install selenium
npm install -g phantomjs-prebuilt

実際のコード

from selenium.webdriver import PhantomJS
import time
driver = PhantomJS()
driver.get("https://translate.google.co.jp/?um=1&ie=UTF-8&hl=ja&client=tw-ob#en/ja/")
def eng2jp(eng_text):
    driver.find_element_by_id("source").clear()
    driver.find_element_by_id("source").send_keys(eng_text)
    driver.find_element_by_id("gt-submit").click()
    time.sleep(0.1) # 少し待つ
    return "".join([i.text for i in driver.find_elements_by_xpath('//span[@id="result_box"]//span')])

eng2jp("lement is no longer attached to the DOM")
# returns '英語を日本語に翻訳する'
0
6
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
0
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?