【发布时间】:2016-03-05 18:41:42
【问题描述】:
目前在这个site 上我正在抓取镜头图信息。为了获取信息,我需要为有多少镜头创建一个 for 循环。我通过点击“球队统计”并找到投篮次数来找到投篮次数。
我想制作正确的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
}
任何帮助将不胜感激。如果需要任何进一步的信息,请告诉我。
【问题讨论】:
-
试试
help('for'),你为什么要标记javascript -
一般来说,如果你不知道终止点的先验,最好使用
while()而不是for()。使用 while,您可以在每次迭代时检查您是否已到达终点。 -
可能需要一段时间
-
seq_along()和朋友可能在这里有用;目前尚不清楚 54 在这里代表什么,但大概是某个预先存在的对象的length()。
标签: r selenium for-loop rselenium