类似这样的内容应该会为您提供一个数据帧,其中所有数据都在单独的列中。
library(RCurl)
library(XML)
# Set link to website
link1 <-("https://publicacionexterna.azurewebsites.net/publicaciones/prices")
# Get data from webpage
data_prices <- getURL(link1)
# Parse XML data
xmlfile <- xmlParse(data_prices)
# Get place nodes
places <- getNodeSet(xmlfile, "//place")
# Get values for each place
values <- lapply(places, function(x){
# Get current place id
pid <- xmlAttrs(x)
# Get values for each gas type for current place
newrows <- lapply(xmlChildren(x), function(y){
# Get type and update time values
attrs <- xmlAttrs(y)
# Get price value
price <- xmlValue(y)
names(price) <- "price"
# Return values
return(c(pid, attrs, price))
})
# Combine rows to single list
newrows <- do.call(rbind, newrows)
# Return rows
return(newrows)
})
# Combine all values into a single dataframe
df <- as.data.frame(do.call(rbind, values), stringsAsFactors = FALSE)
# Reset row names for dataframe
row.names(df) <- c(1:nrow(df))