R如何在不知道长度的情况下制作for循环?
目前在这方面,我在快照图表的信息刮。为了获取信息,无论有多少张照片,我都需要做一个for循环。我通过点击“球队统计”并查找投篮次数来计算投篮次数 我想在不必找出放炮次数的情况下,为循环做出正确的R如何在不知道长度的情况下制作for循环?,r,selenium,for-loop,rselenium,R,Selenium,For Loop,Rselenium,目前在这方面,我在快照图表的信息刮。为了获取信息,无论有多少张照片,我都需要做一个for循环。我通过点击“球队统计”并查找投篮次数来计算投篮次数 我想在不必找出放炮次数的情况下,为循环做出正确的 我目前正在做的事情: shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0), data_period=as.vector(0),
我目前正在做的事情:
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
}
shotchartlibrary(RSelenium)
checkForServer()
startServer()
系统睡眠(5)
重试help('for')
,为什么要标记Javascript一般来说,如果事先不知道终止点,最好使用while()
而不是for()
。使用while,您可以在每次迭代时检查是否已到达终点。这可能需要一段时间继续();现在还不清楚54在这里代表什么,但可能是某个预先存在的对象的length()
。i
怎么会低于0?如果永远不会,则while条件中是否需要此项?这是否会导致元素[1]中的错误:“S4”类型的对象不可再附加?
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
}