Translating Web Page while Scraping

Suppose you need to scrape data from a website after translating the web page in R and Python. In google chrome, there is an option (or functionality) to translate any foreign language. If you are an english speaker and don't know any other foreign language and you want to extract data from the website which does not have option to convert language to English, this article would help you how to perform translation of a webpage.
Translate webpage

What is Selenium?

You may not familiar with Selenium so it is important to understand the background. Selenium is an open-source tool which is very popular in testing domain and used for automating web browsers. It allows you to write test scripts in several programming languages. Selenium is available in both R and Python.

Translate Page in Web Scraping in R and Python

In R there is a package named RSelenium whereas Selenium can be installed by installing selenium package in Python. Following is a list of languages chrome supports along with their code. You need this code in making chrome understand from which language to what language you want to translate the web page.
Name Code
Amharic am
Arabic ar
Basque eu
Bengali bn
English (UK) en-GB
Portuguese (Brazil) pt-BR
Bulgarian bg
Catalan ca
Cherokee chr
Croatian hr
Czech cs
Danish da
Dutch nl
English (US) en
Estonian et
Filipino fil
Finnish fi
French fr
German de
Greek el
Gujarati gu
Hebrew iw
Hindi hi
Hungarian hu
Icelandic is
Indonesian id
Italian it
Japanese ja
Kannada kn
Korean ko
Latvian lv
Lithuanian lt
Malay ms
Malayalam ml
Marathi mr
Norwegian no
Polish pl
Portuguese (Portugal) pt-PT
Romanian ro
Russian ru
Serbian sr
Chinese (PRC) zh-CN
Slovak sk
Slovenian sl
Spanish es
Swahili sw
Swedish sv
Tamil ta
Telugu te
Thai th
Chinese (Taiwan) zh-TW
Turkish tr
Urdu ur
Ukrainian uk
Vietnamese vi
Welsh cy
We are mainly performing 3 actions in the following syntax. First we are loading selenium and specifying languages for translation. Then we are opening a blank page in chrome and navigating to the URL from which we want to extract data. Here it is At the end we are taking snapshot of the webpage.

R Code

You need to install Docker first before running the code below. Go to Products and download Docker Desktop Once downloaded and installed follow the code below.
shell('docker run -d -p 4445:4444 selenium/standalone-chrome')

eCaps <- list(chromeOptions = list(args = c('--disable-gpu','--window-size=1920,1080', '--lang=en'),
                                   prefs = list(translate_whitelists=list('ru' = 'en'),
remDr <- RSelenium::remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "chrome", extraCapabilities = eCaps)

remDr$open(silent = TRUE)
remDr$screenshot(display = TRUE)

In the above program I am translating from Russian language ru to English en

Python Code

Make sure to install chrome driver before running the following python program. Check the version of installed chrome browser on your machine and download the driver accordingly. Specify the file location in the code below where chrome driver is installed.
from selenium import webdriver

myoptions = webdriver.ChromeOptions()

prefs = {
  "translate_whitelists": {"ru":"en"},

myoptions.add_experimental_option("prefs", prefs)
d = webdriver.Chrome('C:/Users/dbhalla/Downloads/chromedriver_win32/chromedriver', options=myoptions)

# Take screenshot 
# Loading the image 
from PIL import Image 
image ="image.png") 
# Showing the image 

# Close the session 
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

0 Response to "Translating Web Page while Scraping "

Post a comment

Next → ← Prev
Love this Post? Spread the Word!