r - For loop skip if table does not exist -


ran in problem. have loop contains urls scrape batting information table id batting_gamelogs. if id not exist on page move on next url else scrape table.

i think should below, can't work.

 if xpathsapply(batting, '//*[@id != "batting_gamelogs"]')[[1]] next   else     {  tablenode <- xpathsapply(batting, '//*[@id="batting_gamelogs"]')[[1]]     data <- readhtmltable(tablenode, stringsasfactors = false)     data # select first table     total <- cbind(id,year,data)      batlist <- rbind(batlist, total)      } 

i have attached sample code.

#scrape batting stats  data = null batlist = null  battingurls <- paste("http://www.baseball-reference.com",yplist[,c("hrefs")],sep="")  for(thisbattingurl in battingurls){  batting <- htmlparse(thisbattingurl)  fstampid <- regexpr("&", thisbattingurl, fixed=true)-1 fstampyr <- regexpr("year=", thisbattingurl, fixed=true)+5 id <- substr(thisbattingurl, 53, fstampid) year <- substr(thisbattingurl, fstampyr, 75)  tablenode <- xpathsapply(batting, '//*[@id="batting_gamelogs"]')[[1]] data <- readhtmltable(tablenode, stringsasfactors = false) data # select first table total <- cbind(id,year,data)  batlist <- rbind(batlist, total)  }  batlist 

any appreciated!

i can't work.

this phrase should reminder tell happened (and how differs expected happen). suspect happened skipped (vs. not skipping when should have). tell that, instead of leaving figure out.

if xpathsapply(batting, '//*[@id != "batting_gamelogs"]')[[1]] next 

the "not" in wrong place. here, you're saying, skip iteration if there element on page has id attribute value not batting_gamelogs. instead want skip iteration if there no element on page has id attribute value is batting_gamelogs.

so, use xpath expression:

'//*[@id = "batting_gamelogs"]' 

and put "not" outside of xpathsapply(), testing whether length of result list 0 (thanks answer @ https://stackoverflow.com/a/25553805/423105):

if (length(xpathsapply(batting, '//*[@id = "batting_gamelogs"]')) == 0) next 

i took out [[1]] because want test whether values returned; don't care extracting first result.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -