Google translate via web scraping r

2019-06-04 00:55发布

I have a list of 1000 text in Russian language and want to convert it to English in R. I know there are some R packages for google translate but that requires API. And getting google API is paid now. In Excel VBA, I have a macro which visits google translate website and converts it. See the URL and parameters below -

getParam = "Прием (осмотр, консультация) врача-инфекциониста первичный"
translateFrom = "ru"
translateTo = "en"

URL = "https://translate.google.pl/m?hl=" & translateFrom & "&sl=" & translateFrom & "&tl=" & translateTo & "&ie=UTF-8&prev=_m&q=" & getParam

Can same thing be done in R?

标签: r rvest
1条回答
对你真心纯属浪费
2楼-- · 2019-06-04 01:34

Here is a solution ,

library(RCurl)
library(XML)

getParam = "Прием (осмотр, консультация) врача-инфекциониста первичный"
translateFrom = "ru"
translateTo = "en"

search <- gsub(" ", "%20", getParam)

URL <- paste("https://translate.google.pl/m?hl=",translateFrom,"&sl=",translateFrom,"&tl=",translateTo,"&ie=UTF-8&prev=_m&q=",search,sep="")

page <- getURL(URL)

tree <-htmlTreeParse(page)

body <- tree$children$html$children$body 

body_text <- body$children[[5]]$children[[1]]  

print(body_text) 

You can find more information about web parsing from this question.

查看更多
登录 后发表回答