0

I am trying to automatically translate a text from different language to English.

i am doing the passages explained on this other question: Google translate via web scraping r

but I obtain this error:

Error in function (type, msg, asError = TRUE)  :  Illegal characters found in URL

my code is

 getParam <- as.character(db$text) 
 translateFrom <- as.character(db$language)

 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="", ssl.verifypeer = FALSE)

 page <- getURL(URL)  

 tree <- htmlTreeParse(page)

 body <- tree$children$html$children$body 
Silvia
  • 405
  • 4
  • 17
  • What's your URL? Please try to make this reproducible by including (a sample of) your data and your exact code (even if a copy from another question). – r2evans Nov 02 '17 at 18:12
  • @r2evans something like this? is it ok? – Silvia Nov 02 '17 at 18:20
  • No. To see if it is okay, start a "fresh" R instance (with no variables or packages loaded) and try to run your code. If you get `object '...' not found` or `count not find function "..."`, then your answer is not sufficiently reproducible. In this case, we have no idea what `db` contains. – r2evans Nov 02 '17 at 20:51

1 Answers1

0
library(XML)
library(RCurl)

db=data.frame(text = c("traduire", "tradurre"), langage=c("fr", "it"))

ls = unlist(apply(db, 1, list), recursive = FALSE)

lapply(unlist(apply(db, 1, list), recursive = FALSE), function(x){

  getParam <- as.character(x[1])
  translateFrom <- as.character(x[2])

  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]]
  body_text

})