问题描述
我有一个来自特定域(pixilink.com)的URL列表(超过4000个),我要做的是弄清楚所提供的域是图片还是视频。为此,我使用了此处提供的解决方案:How to write trycatch in R和Check whether a website provides photo or video based on a pattern in its URL并编写了如下所示的代码:
#Function to get the value of initial_mode from the URL
urlmode <- function(x){
mycontent <- readLines(x)
mypos <- grep("initial_mode = ",mycontent)
if(grepl("0",mycontent[mypos])){
return("picture")
} else if(grepl("tour",mycontent[mypos])){
return("video")
} else{
return(NA)
}
}
此外,为了防止出现不存在的URL错误,我使用了以下代码:
readUrl <- function(url) {
out <- tryCatch(
{
readLines(con=url,warn=FALSE)
return(1)
},error=function(cond) {
return(NA)
},warning=function(cond) {
return(NA)
},finally={
message( url)
}
)
return(out)
}
最后,我分离了URL列表,并将其传递给上述函数:
a <- subset(new_df,new_df$host=="www.pixilink.com")
vec <- a[['V']]
vec <- vec[1:1000] # only chose first 1000 rows
tt <- numeric(length(vec)) # checking validity of url
for (i in 1:length(vec)){
tt[i] <- readUrl(vec[i])
print(i)
}
g <- data.frame(vec,tt)
g2 <- g[which(!is.na(g$tt)),] #only valid url
dd <- numeric(nrow(g2))
for (j in 1:nrow(g2)){
dd[j] <- urlmode(g2[j,1])
}
Final <- cbind(g2,dd)
Final <- left_join(g,Final,by = c("vec" = "vec"))
我在包含100个URL的示例URL列表中运行了此代码,并且有效;但是,在整个网址列表上运行它之后,它返回了错误。这是错误:Error in textConnection("rval","w",local = TRUE) : all connections are in use Error in textConnection("rval",local = TRUE) : all connections are in use
在此之后,甚至对于示例URL(我之前测试过的100个示例),我都运行代码并收到以下错误消息:Error in file(con,"r") : all connections are in use
任何人都可以解释此错误的含义吗?它与我们可以从网站上提出的请求数量有关吗?有什么解决方案?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)