小编tom*_*tom的帖子

使用XML/RCurl R包解析HTML表,而不使用readHTMLTable函数

我试图从以下单个html表中提取/提取数据:http://www.theplantlist.org/tpl/record/kew-419248和一些非常相似的页面.我最初尝试使用以下函数来读取表格,但它并不理想,因为我想将每个物种名称分成其组成部分(属/物种/种类/作者等).

library(XML)
readHTMLTable("http://www.theplantlist.org/tpl/record/kew-419248")
Run Code Online (Sandbox Code Playgroud)

我使用SelectorGadget为每个要提取的表元素标识一个唯一的XPATH(不一定是最短的):

对于属名:// [contains(concat("",@ class,""),concat("","synonym",""))] // [contains(concat("",@ class,"" ),concat("","genus",""))]

对于物种名称:// [contains(concat("",@ class,""),concat("","Synonym",""))] // [contains(concat("",@ class,"" ),concat("","species",""))]

对于infraspecies排名://*[含有(CONCAT( "",@class, ""),CONCAT( "", "infraspr", ""))]

对于infraspecies名称://*[contains(concat("",@ class,""),concat("","infraspe",""))]

对于置信水平(图像):// [contains(concat("",@ class,""),concat("","synonyms",""))] // img对于sources:// [contains(concat) ("",@ class,""),concat("","source",""))] // a

我现在想要将信息提取到数据帧/表中.

我尝试使用XML包的xpathSApply函数来提取一些这样的数据:

例如,对于种类的排名

library(XML)
library(RCurl)
infraspeciesrank = htmlParse(getURL("http://www.theplantlist.org/tpl/record/kew-419248"))
path=' //*[contains(concat( " ", @class, " " ), concat( " ", "infraspr", " " ))]'
xpathSApply(infraspeciesrank, path)
Run Code Online (Sandbox Code Playgroud)

然而,这种方法是因为在数据空白的问题(例如,仅表的某些行有infraspecies排名,因此,所有我回来是在表中的三个等级的名单,没有间隙).数据输出也是我无法附加到数据帧的类.

有谁知道从这个表中提取信息到数据帧的更好方法?

任何帮助将非常感激!

汤姆

xml r html-parsing web-scraping rcurl

3
推荐指数
1
解决办法
5573
查看次数

标签 统计

html-parsing ×1

r ×1

rcurl ×1

web-scraping ×1

xml ×1