加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

curl :: curl_fetch_memory(url,handle = handle)出错:从对等端

发布时间:2020-12-14 02:20:19 所属栏目:Linux 来源:网络整理
导读:我正在尝试从 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
我正在尝试从 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)
}

我收到以下错误:

Error in curl::curl_fetch_memory(url,handle = handle) : Failure when receiving data from the peer

解决方法

RETRY功能是在 June 2016中引入的,允许您多次重试请求,直到成功为止.

将它与参数verb =“GET”一起使用,而不是直接使用GET,即

data <- RETRY("GET",query)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读