1

我想从这个 html 中挑选:

doc <- htmlParse("http://eusoils.jrc.ec.europa.eu/ESDB_Archive/ESDBv3/legend/sg_attr.htm")

但我对特殊字符(即 > 和 < 符号)有一些问题,并获得不同长度的节点,请参见此处:

legs <- getNodeSet(doc, "//a")
leg_names <- sapply(legs, xmlGetAttr, "name")
leg_descr <- xpathSApply(doc, "//strong", xmlValue)

# not the same length??
cbind(leg_names, leg_descr)

# different length??
getNodeSet(doc, '//text()[following-sibling::a]')

# why is this not working?
getNodeSet(doc, '//a[@name="AGLIM1"]/text()[following-sibling::strong')

最后,我希望将每个图例(具有特定名称的 a 标记之后的文本)放在一个有两列的表中,第一列带有值/符号,第二列带有它的标签..

像 WRB-FULL 的这个:

     Value                  Label
        AB            Albeluvisol
      ABal       Alic Albeluvisol
      ABap   Abruptic Albeluvisol
      ABar     Arenic Albeluvisol
      ABau     Alumic Albeluvisol
     ABeun Endoeutric Albeluvisol
       ...        ...         ...
4

1 回答 1

0

文档的格式不一致:有些<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" 
于 2013-05-06T07:20:32.500 回答