R如何在不知道长度的情况下制作for循环?
R How to make a for loop without knowing the length?
目前在这个 site 我正在收集镜头图表信息。为了抓取信息,我需要为有很多镜头制作一个 for 循环。我通过单击 "Team Stats" 并查找投篮尝试次数来查找投篮次数。
我想做出正确的for loop
,而不必找出拍摄的数量。
我目前在做什么:
shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0),
data_period=as.vector(0), player_id=as.vector(0), data_text=as.vector(0),
location=as.vector(0), gamenumber= as.vector(0))
for (i in 1:54)
{
text <-paste0("//*[(@class='shots home-team')]//*[(@id)][",i,"]")
shotchart[nrow(shotchart)+1,1]<- unlist(re$findElement(using='xpath',text)$getElementAttribute('id'))
shotchart[nrow(shotchart),2]<- unlist(re$findElement(using='xpath', text)$getElementAttribute('class'))
shotchart[nrow(shotchart),3]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-homeaway'))
shotchart[nrow(shotchart),4]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-period'))
shotchart[nrow(shotchart),5]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-shooter'))
shotchart[nrow(shotchart),6]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-text'))
shotchart[nrow(shotchart),7]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('style'))
shotchart[nrow(shotchart),8]<-k-1
}
如有任何帮助,我们将不胜感激。如果需要任何进一步的信息,请告诉我。
library(RSelenium)
checkForServer()
startServer()
Sys.sleep(5)
re<-remoteDriver()
re$open()
re$navigate("http://espn.go.com/mens-college-basketball/playbyplay?gameId=400830392")
shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0),
data_period=as.vector(0), player_id=as.vector(0), data_text=as.vector(0),
location=as.vector(0), gamenumber= as.vector(0))
error="Error : \t Summary: NoSuchElement\n \t Detail: An element could not be located on the page using the given search parameters.\n \t class: org.openqa.selenium.NoSuchElementException\n"
i<-1
element=0
while ((i>0)&(element[1]!=error))
{
text <-paste0("//*[(@class='shots home-team')]//*[(@id)][",i,"]")
element<- try(unlist(re$findElement(using='xpath', text)$getElementAttribute('id')),silent = TRUE)
if (element[1]==error)
break;
shotchart[nrow(shotchart)+1,1]<- unlist(re$findElement(using='xpath',text)$getElementAttribute('id'))
shotchart[nrow(shotchart),2]<- unlist(re$findElement(using='xpath', text)$getElementAttribute('class'))
shotchart[nrow(shotchart),3]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-homeaway'))
shotchart[nrow(shotchart),4]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-period'))
shotchart[nrow(shotchart),5]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-shooter'))
shotchart[nrow(shotchart),6]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-text'))
shotchart[nrow(shotchart),7]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('style'))
shotchart[nrow(shotchart),8]<-i-1
i<-i+1
}
我不确定你的变量 k 是什么意思。
我听取了所有评论的建议。
目前在这个 site 我正在收集镜头图表信息。为了抓取信息,我需要为有很多镜头制作一个 for 循环。我通过单击 "Team Stats" 并查找投篮尝试次数来查找投篮次数。
我想做出正确的for loop
,而不必找出拍摄的数量。
我目前在做什么:
shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0),
data_period=as.vector(0), player_id=as.vector(0), data_text=as.vector(0),
location=as.vector(0), gamenumber= as.vector(0))
for (i in 1:54)
{
text <-paste0("//*[(@class='shots home-team')]//*[(@id)][",i,"]")
shotchart[nrow(shotchart)+1,1]<- unlist(re$findElement(using='xpath',text)$getElementAttribute('id'))
shotchart[nrow(shotchart),2]<- unlist(re$findElement(using='xpath', text)$getElementAttribute('class'))
shotchart[nrow(shotchart),3]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-homeaway'))
shotchart[nrow(shotchart),4]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-period'))
shotchart[nrow(shotchart),5]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-shooter'))
shotchart[nrow(shotchart),6]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-text'))
shotchart[nrow(shotchart),7]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('style'))
shotchart[nrow(shotchart),8]<-k-1
}
如有任何帮助,我们将不胜感激。如果需要任何进一步的信息,请告诉我。
library(RSelenium)
checkForServer()
startServer()
Sys.sleep(5)
re<-remoteDriver()
re$open()
re$navigate("http://espn.go.com/mens-college-basketball/playbyplay?gameId=400830392")
shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0),
data_period=as.vector(0), player_id=as.vector(0), data_text=as.vector(0),
location=as.vector(0), gamenumber= as.vector(0))
error="Error : \t Summary: NoSuchElement\n \t Detail: An element could not be located on the page using the given search parameters.\n \t class: org.openqa.selenium.NoSuchElementException\n"
i<-1
element=0
while ((i>0)&(element[1]!=error))
{
text <-paste0("//*[(@class='shots home-team')]//*[(@id)][",i,"]")
element<- try(unlist(re$findElement(using='xpath', text)$getElementAttribute('id')),silent = TRUE)
if (element[1]==error)
break;
shotchart[nrow(shotchart)+1,1]<- unlist(re$findElement(using='xpath',text)$getElementAttribute('id'))
shotchart[nrow(shotchart),2]<- unlist(re$findElement(using='xpath', text)$getElementAttribute('class'))
shotchart[nrow(shotchart),3]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-homeaway'))
shotchart[nrow(shotchart),4]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-period'))
shotchart[nrow(shotchart),5]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-shooter'))
shotchart[nrow(shotchart),6]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-text'))
shotchart[nrow(shotchart),7]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('style'))
shotchart[nrow(shotchart),8]<-i-1
i<-i+1
}
我不确定你的变量 k 是什么意思。
我听取了所有评论的建议。