Error in curl :: curl_fetch_memory (url, handle = handle): Error while receiving data from a partner

I am trying to get texts from the graphical API . I am writing an R function that works, but not inside a loop. My script:

library(httr) library(RCurl) library(XML) df <- data.frame(artist = c('Led Zeppellin', 'Adele'), song = c('RockΒ΄n roll', 'Hello'), stringsAsFactors = F) make.querrye <- function(xx) { names_ok <- gsub(" ", "&", xx) names_ok2 <- paste("\'", names_ok, "\'", sep = '') querrye <- paste("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=", names_ok[1],"&song=", names_ok[2], sep='') data <- GET(querrye) aa <- content(data, "text") doc <- htmlParse(aa, asText=TRUE) plain.text <- xpathSApply(doc, "//lyric//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue) if (length(plain.text)==0) { plain.text2 <- 'Lyrics not found' } else { plain.text2 <- iconv(plain.text, from = "UTF-8", to = "latin1", sub = NA, mark = TRUE, toRaw = FALSE) } return(plain.text2) } names <- c(df$artist[1], df$song[1]) make.querrye(names) #- it works names <- c(df$artist[2], df$song[2]) make.querrye(names) #- it also works 

But my function does not work inside the loop

 for (ii in 1:2){ names <- c(df$artist[ii], df$song[ii]) print(names) make.querrye(names) } 

I get the following error:

Error in curl :: curl_fetch_memory (url, handle = handle): Error while receiving data from a peer

+7
loops r rcurl httr
source share
1 answer

The RETRY function was introduced in June 2016 and allows you to repeat the request several times until it succeeds.

Use it with the verb = "GET" parameter instead of directly using GET , i.e.

 data <- VERB("GET", querrye) 

You can also determine the maximum number of attempts with the times parameter.

+1
source share

All Articles