gpt4 book ai didi

r - 在curl::curl_fetch_memory(url,handle=handle)中出错:从对等方接收数据时失败

转载 作者:行者123 更新时间:2023-12-02 03:35:05 27 4
gpt4 key购买 nike

我正在尝试从 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 参数定义最大尝试次数。

关于r - 在curl::curl_fetch_memory(url,handle=handle)中出错:从对等方接收数据时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276457/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com