Created
December 20, 2012 22:15
-
-
Save fawda123/4349045 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#install the packages | |
install.packages('XML') | |
require(XML) | |
#get raw html for Lake Minnetonka | |
html.raw<-htmlTreeParse( | |
'http://www.dnr.state.mn.us/lakefind/showreport.html?downum=27013300', | |
useInternalNodes=T | |
) | |
#parse html using <p> tag | |
html.parse<-xpathApply(html.raw, path="//p", fun=xmlValue) | |
#find elements that containt 'Depth' | |
robj.parse<-grep('*Depth*',unlist(html.parse),value=T) | |
#get correct element with maximum depth info | |
robj.parse<-robj.parse[[1]] | |
#use 'strsplit' to get actual value for maximum depth | |
depth.parse<-as.numeric(strsplit(strsplit(robj.parse,'ft): ')[[1]][2],'Water')[[1]][1]) | |
#combine above code into a single function | |
depth.fun<-function(lake){ | |
url.in<-paste( | |
'http://www.dnr.state.mn.us/lakefind/showreport.html?downum', | |
lake, | |
sep='=' | |
) | |
html.raw<-htmlTreeParse(url.in,useInternalNodes=T) | |
html.parse<-xpathApply(html.raw, path="//p", fun=xmlValue) | |
robj.parse<-grep('*Depth*',unlist(html.parse),value=T) | |
depth.parse<-as.numeric(strsplit(strsplit(robj.parse,'ft): ')[[1]][2],'Water')[[1]][1]) | |
return(depth.parse) | |
} | |
#examples showing how the function is used | |
depth.fun('27013300') | |
lake.ids<-c('27013700','82004600','82010400') | |
sapply(lake.ids,depth.fun) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment