本文介绍了R如何在不知道长度的情况下做出for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 目前在这个网站上我在抓拍图表信息。为了抓取信息,我需要做一个for循环,但是有很多镜头。我通过点击球队统计找到了投篮次数,并找到了投篮命中次数。 我想使用来表示 我现在在做什么: $ b $循环 b shotchart data_period = as.vector(0),player_id = as.vector(0),data_text = as.vector(0), location = as.vector(0),gamenumber = as.vector(0) ) (i 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')) [nrow(shotchart),3]< -unlist(re $ findElement(using ='xpath',text)$ getE lementAttribute('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 (使用='xpath',text)$ getElementAttribute('style')) shotchart [nrow(shotchart),8]< -k-1 } 任何帮助将不胜感激。请让我知道是否需要进一步的信息。解决方案 library(RSelenium) checkForServer() startServer() Sys.sleep(5) re 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 =错误:\ t摘要:NoSuchElement \ n \ t详细信息:元素无法找到org.openqa.selenium.NoSuchElementException\\\ i element = 0 while((i> ; 0)&(element [1]!= error)) { text< -paste0(// * [(@ class ='shots home-team')] // * [(@id)] [ I,] )元素< - 尝试(unlist(re $ findElement(using ='xpath',text)$ getElementAttribute('id')),silent = TRUE) if(element [1] ==错误) 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使用='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的意思。 我从所有的意见中得到了建议。 Currently on this site I am scraping in the shot chart information. To scrape in the info I need to make a for loop for however many shots there are. I find the number of shots by clicking "Team Stats" and finding the number of field goal attempts.I would like to make the proper for loop without having to find out the number of shots.What I am currently doing: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}Any help will be appreciated. Please let me know if any further information is needed. 解决方案 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<-1element=0while ((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}I am not sure as what your variable k means.I took advice from all the comments. 这篇关于R如何在不知道长度的情况下做出for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-06 13:03