Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从XMLDocumentContent对象提取$children$html内容_Html_Xml_R - Fatal编程技术网

如何从XMLDocumentContent对象提取$children$html内容

如何从XMLDocumentContent对象提取$children$html内容,html,xml,r,Html,Xml,R,提前道歉,我相信这很简单,但我不知道我做错了什么 除其他事项外,此代码 study.name <- 'NLSY79' library(XML) library(httr) sub.study <- paste0( "https://www.nlsinfo.org/investigator/servlet1?get=SUBSTUDIES&study=" , study.name ) study.html <- GET( sub.study ) content( study

提前道歉,我相信这很简单,但我不知道我做错了什么

除其他事项外,此代码

study.name <- 'NLSY79'
library(XML)
library(httr)
sub.study <- paste0( "https://www.nlsinfo.org/investigator/servlet1?get=SUBSTUDIES&study=" , study.name )
study.html <- GET( sub.study )
content( study.html )
study.block <- htmlParse( study.html , asText = TRUE )

study.name您可以使用
xpathsaply
提取所需的元素

xpathSApply(study.block, "//option")
# [[1]]
# <option value="-1" selected="selected">(Choose One)</option> 
# [[2]]
# <option value="343.06">NLSY79 (1979-2010)</option> 

您可以使用
xpathsaply
提取所需的元素

xpathSApply(study.block, "//option")
# [[1]]
# <option value="-1" selected="selected">(Choose One)</option> 
# [[2]]
# <option value="343.06">NLSY79 (1979-2010)</option> 

您还可以使用
xmlGetAtrr

xpathSApply(study.block, "//option", xmlGetAttr, "value")
[1] "-1"     "343.06"


您还可以使用
xmlGetAtrr

xpathSApply(study.block, "//option", xmlGetAttr, "value")
[1] "-1"     "343.06"

xpathSApply(study.block, "//option[not(@selected)]", xmlGetAttr, "value")
[1] "343.06"