【问题标题】:Error in curl::curl_fetch_memory(url, handle = handle) : Failure when receiving data from the peercurl::curl_fetch_memory(url, handle = handle) 中的错误:从对等方接收数据时失败
【发布时间】:2016-11-11 14:25:39
【问题描述】:

我正在尝试从chartlyrics API 获取歌词。我编写了一个可以工作但不在循环内的 R 函数。我的脚本是:

library(httr)
library(RCurl)
library(XML)

df <- data.frame(artist = c('Led Zeppellin', 'Adele'), song = c('Rock´n roll', 'Hello'), stringsAsFactors = F)

make.querye <- function(xx) {
  names_ok <- gsub(" ", "&", xx)
  names_ok2 <- paste("\'", names_ok, "\'", sep = '')
 querye <- paste("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=", names_ok[1],"&song=",  names_ok[2], sep='')
 data <- GET(querye)
 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.querye(names) #- it works

names <- c(df$artist[2], df$song[2])
make.querye(names) #- it also works

但我的函数在循环中不起作用

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

我收到以下错误:

curl::curl_fetch_memory(url, handle = handle) 中的错误:从对端接收数据时失败

【问题讨论】:

  • 你在代理后面吗?
  • 对不起,我今天看到了你的评论。事实上,我不确定你的问题。是一个非常古老的问题,现在我没有解决这个问题,那么我将关闭这个问题(如果可以的话,因为以前从未这样做过)。在很长一段时间内,我看到其他人在做类似的事情,我认为解决方案是在请求之间放置一些时间,但我不确定这一点。无论如何,谢谢你的回答

标签: r loops rcurl httr


【解决方案1】:

RETRY 函数是在June 2016 中引入的,允许您多次重试请求,直到成功。

与参数verb = "GET"一起使用,而不是直接使用GET,即,

data <- RETRY("GET", query)

您还可以使用times 参数定义最大尝试次数。

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2020-03-03
    • 1970-01-01
    • 2018-12-26
    • 2020-03-31
    • 2017-12-14
    • 2021-11-26
    • 2015-10-22
    • 2020-09-20
    相关资源
    最近更新 更多