Forcibly use Google Translate from python

motivation

If you copy and paste the text when translating an English dissertation, strange line breaks and characters may be inserted depending on the pdf. I thought it would be easier if I translated it together when formatting that area with python, so that method

Method

Google Translate api is paid, so emulate the browser with selenium and use Google Translate of the browser

Preparation

pip install selenium
npm install -g phantomjs-prebuilt

Actual code

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) #wait a bit
    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 'Translate English to Japanese'

Recommended Posts

Forcibly use Google Translate from python
Use Google Analytics API from Python
Use Google Cloud Vision API from Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Use MySQL from Anaconda (python)
Use e-Stat API from Python
Use Stanford Core NLP from Python
[Python3] Google translate google translate without using api
Read and use Python files from Python
Use kabu Station® API from Python
Use Azure Blob Storage from Python
Use the Flickr API from Python
Use fastText trained model from Python
Use PostgreSQL data type (jsonb) from Python
How to update Google Sheets from Python
Use machine learning APIs A3RT from Python
I want to use jar from python
Use Django from a local Python script
Use C ++ functions from python with pybind11
sql from python
MeCab from Python
[Python] Download original images from Google Image Search
Firebase: Use Cloud Firestore and Cloud Storage from Python
Study from Python Hour7: How to use classes
[Bash] Use here-documents to get python power from bash
Wrap C with Cython for use from Python
Use Python in your environment from Win Automation
I want to use ceres solver from python
Google Colaboratory 90-minute session disconnection countermeasures --- Use Python! ---
Let's use different versions of SQLite3 from Python3!
Wrap C ++ with Cython for use from Python
Use the nghttp2 Python module from Homebrew from pyenv's Python
Use Tor to connect from urllib2 [Python] [Mac]
Python: Use zipfile to unzip from standard input
I tried using Google Translate from Python and it was just too easy
Use R density ratio estimation package densratio from Python
Touch MySQL from Python 3
Use config.ini in Python
Operate Filemaker from Python
[Python] Use JSON with Python
Use dates in Python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python] Easy Google Translate app using Eel and Googletrans
Python from or import
Use Valgrind in Python
Use mecab with Python3
Use LiquidTap Python Client ③
Run python from excel
Install python from source
Use DynamoDB with Python
Execute command from Python
I wanted to use the Python library from MATLAB
From python to running instance on google cloud platform
Operate neutron from Python!
Use Python 3.8 with Anaconda