R and readLines of webpage text

好久不见. 提交于 2019-12-11 23:06:50

问题


I am wanting to create a single dataframe from the following website: http://www.arrs.net/MaraList/ML_2014.htm

Unfortunately I am not sure how to take what seem to be tab delimiters and create columns of data. The code I have below takes and creates multiple character strings but I'm having trouble determining how to separate names that have multiple words into a single column as it is shown on the site.

library(XML)
url<-"http://www.arrs.net/MaraList/ML_2014.htm"
data<-readLines(url)
data<-sub("</FONT></b><FONT SIZE=\"2\" <FONT COLOR=\"#00000\" FACE=\"Courier New,           Courier\">","",data)
data<-sub("<B><FONT COLOR=\"#0066FF\" FACE=\"Arial\">","",data)
data<-read.table(textConnection(data),stringsAsFactors=FALSE)
data<-data[11:40000,1]

So, not sure any of the current code I have can get me there. Any information or link(s) to prior posts would be appreciated.


回答1:


Here's one approach to read this in (using two packages I maintain and the terrific stacksplitshape package) . You'll need the dev version of qdapTools.

devtools::install_github("trinker/qdapTools")
library(qdapTools); library(qdapRegex); library(splitstackshape)
url<-"http://www.arrs.net/MaraList/ML_2014.htm"

m <- readLines(url)[-c(1:7, 2760:2767)]

## Split into lists by country    
x <- loc_split(m, unique(grep("<B><FONT", m)))

## Clean up country names
nms <- rm_angle(sapply(x, `[`, 1))

## remove html country name from data can convert to a data.frame
dat <- list2df(setNames(lapply(x, `[`, -1), nms), "dats", "Country")[, 2:1]

## Use hand parsing technique to locate widths      
## I added a # before each column in row one of data
## gregexpr tells us the location of the # characters
det <- "AAR  #26#Jan #King George Island           #      #27+25   #White Continent                        #4:03:30    #Steve Hibbs (USA)              #4:13:02    #Suzy Seeley (54,TX/USA) "
widths <- gregexpr("#", det)[[1]]

## replace those widths with # character as it is not any where else in data set
for (i in widths){
    substring(dat[["dats"]], i, i) <-  "#" 
}

## split columns on # character
out <- cSplit(dat, 2, sep="#")

out


来源:https://stackoverflow.com/questions/27157103/r-and-readlines-of-webpage-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!