文档的格式不一致:有些<a>
元素没有后面的<strong>
元素——所以前者更多。
cbind( head(leg_names,8), head(leg_descr,8) )
[,1] [,2]
# [1,] "AGLIM1" "AGLIM1: Code of the most important limitation to agricultural use of the STU"
# [2,] "AGLIM2" "AGLIM2: Code of a secondary limitation to agricultural use of the STU"
# [3,] "BORDER_SOIL1M" "FAO85-FULL: Full Soil Code 1974 FAO"
# [4,] "SOIL1M" "FAO85-LEV1: Soil major group code of the STU from the 1974 (modified CEC 1985) FAO-UNESCO Soil Legend"
# [5,] "CFL" "FAO85-LEV2: Second level soil code of the STU from the 1974 (modified CEC 1985) FAO-UNESCO Soil Legend"
# [6,] "CL" "FAO85-LEV3: Third level soil code of the STU from the 1974 (modified CEC 1985) FAO-UNESCO Soil Legend"
# [7,] "COUNTRY" "FAO90-FULL:Full soil code of the STU from the 1990 FAO-UNESCO Soil Legend"
# [8,] "FAO85FU" "FAO90-LEV1: Soil major group code of the STU from the 1990 FAO-UNESCO Soil Legend"
这种following-sibling
方法似乎更有希望,但是由于<a>
元素没有立即跟在一个<strong>
元素后面,因此您最终可以得到另一个元素的描述。
getNodeSet(doc, '//a[@name="AGLIM1"]/following-sibling::strong/text()')[[1]]
另一种方法是忘记格式并将文件视为文本文件。
raw_data <- readLines("http://eusoils.jrc.ec.europa.eu/ESDB_Archive/ESDBv3/legend/sg_attr.htm")
library(stringr)
matches <- str_extract(raw_data, '<a .*<strong>.*')
matches <- matches[ ! is.na(matches) ]
result <- str_match(matches, '<a name="(.*?)".*<strong>(.*)</strong>')[,-1]
head(result)
[,1] [,2]
[1,] "AGLIM1" "AGLIM1: Code of the most important limitation to agricultural use of the STU"
[2,] "AGLIM2" "AGLIM2: Code of a secondary limitation to agricultural use of the STU"
[3,] "FAO85FU" "FAO85-FULL: Full Soil Code 1974 FAO"
[4,] "FAO85LV1" "FAO85-LEV1: Soil major group code of the STU from the 1974 (modified CEC 1985) FAO-UNESCO Soil Legend"
[5,] "FAO85LV2" "FAO85-LEV2: Second level soil code of the STU from the 1974 (modified CEC 1985) FAO-UNESCO Soil Legend"
[6,] "FAO85LV3" "FAO85-LEV3: Third level soil code of the STU from the 1974 (modified CEC 1985) FAO-UNESCO Soil Legend"