将节点{xml_nodeset}转换为xml或html

将节点{xml_nodeset}转换为xml或html,html,r,xml-parsing,rvest,xml2,Html,R,Xml Parsing,Rvest,Xml2,以xml2::read_html/rvest生成的任何假设节点为例,例如: require(xml2); require(rvest) doc = read_html('https://www.w3.org/TR/html4/intro/intro.html') e = html_node(doc, '.subtoc') 有没有办法将e转换回html?我能做的最接近的事情是: xml2::write_html(e, 'e.html') head(readLines('e.html')) [1]

xml2::read_html
/
rvest
生成的任何假设节点为例,例如:

require(xml2); require(rvest)
doc = read_html('https://www.w3.org/TR/html4/intro/intro.html')
e = html_node(doc, '.subtoc')
有没有办法将
e
转换回html?我能做的最接近的事情是:

xml2::write_html(e, 'e.html')
head(readLines('e.html'))
[1] "<div class=\"subtoc\">"                                               
[2] "<p><strong>Contents</strong></p>"                                     
[3] ""                                                                     
[4] "<ol>"                                                                 
[5] "<li>"                                                                 
[6] "<a class=\"tocxref\" href=\"#h-2.1\">What is the World Wide Web?</a> "
xml2::编写html(e'e.html')
head(readLines('e.html'))
[1] ""                                               
[2] “内容”
[3] ""                                                                     
[4] ""                                                                 
[5] “
  • ” [6] " "
  • 您可以尝试将节点转换为字符,然后重复读取html代码

    newdoc <-read_html(as.character(e))
    
    write_html(newdoc, e.html)
    
    newdoc