Home > Back-end >  Translating Chrome Page using Selenium is not working
Translating Chrome Page using Selenium is not working

Time:01-30

I'm trying to translate a webpage from Japanese to English, below is a code snippet, that used to work 2 weeks before and is not working anymore.

# Initializing Chrome reference

chrome_path = Service("C:\Python\Anaconda\chromedriver.exe")
custom_options = webdriver.ChromeOptions()

prefs = {
  "translate_whitelists": {"ja":"en"},
  "translate":{"enabled":"True"}
}
custom_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(service = chrome_path, options=custom_options)

I've also tried

custom_options.add_argument('--lang=en')
driver = webdriver.Chrome(service = chrome_path, options=custom_options)

None of these seem to work anymore, no other changes to this part of the code have been made. The website I'm trying to translate is https://media.japanmetaldaily.com/market/list/ Any help is greatly appreciated.

CodePudding user response:

I can't get that to work via Selenium/Chrome driver either... You could try use a hidden google translate api https://danpetrov.xyz/programming/2021/12/30/telegram-google-translate.html

I've put it into some python code that translates that table for you using that hidden api with source language "ja" and target language "en":

import urllib.parse
from bs4 import BeautifulSoup
import requests

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
    }

jap_page = requests.get('https://media.japanmetaldaily.com/market/list/',headers=headers)
soup = BeautifulSoup(jap_page.text,'html.parser')
table = soup.find('table',class_='rateListTable')

for line in table.find_all('a'):

    endpoint = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=ja&tl=en&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=7&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&q="
    text = urllib.parse.quote_plus(line.text.strip())

    data = requests.get(endpoint text).json()
    print(data[0][0][1]  ' = ' data[0][0][0])

CodePudding user response:

Try chrome browser and chromedriver version 96. In ver 97 not working

  •  Tags:  
  • Related