使用R中的rvest来抓取一个网页,我想从节点中提取相当于innerHTML
的内容,特别是在应用html_text
之前将换行符更改为换行符。
所需功能的示例:
library(rvest)
doc <- read_html('<html><p class="pp">First Line<br />Second Line</p>')
innerHTML(doc, ".pp")
应产生以下输出:
[1] "<p class=\"pp\">First Line<br>Second Line</p>"
有了rvest 0.2
,这可以通过toString.XMLNode
实现
# run under rvest 0.2
library(XML)
html('<html><p class="pp">First Line<br />Second Line</p>') %>%
html_node(".pp") %>%
toString.XMLNode
[1] "<p class=\"pp\">First Line<br>Second Line</p>"
随着更新的rvest 0.2.0.900
,这不再适用。
# run under rvest 0.2.0.900
library(XML)
html_node(doc,".pp") %>%
toString.XMLNode
[1] "{xml_node}\n<p>\n[1] <br/>"
所需的功能通常在包write_xml
的xml2
函数中可用,rvest
现在依赖于该函数 - 如果只有write_xml
可以将其输出提供给变量而不是坚持写入文件。 (也不接受textConnection
)。
作为一种解决方法,我可以暂时写入文件:
# extract innerHTML, workaround: write/read to/from temp file
html_innerHTML <- function(x, css, xpath) {
file <- tempfile()
html_node(x,css) %>% write_xml(file)
txt <- readLines(file, warn=FALSE)
unlink(file)
txt
}
html_innerHTML(doc, ".pp")
[1] "<p class=\"pp\">First Line<br>Second Line</p>"
然后,我可以将换行标记转换为换行符:
html_innerHTML(doc, ".pp") %>%
gsub("<br\\s*/?\\s*>","\n", .) %>%
read_html %>%
html_text
[1] "First Line\nSecond Line"
有没有更好的方法来实现这一点与现有的功能,例如, rvest
,xml2
,XML
或其他套餐?特别是我想避免写入硬盘。
正如@ r2evans所指出的那样,as.character(doc)
就是解决方案。
关于你最后的代码片段,它想要在将<br>
转换为换行符时从节点中提取<br>
分隔的文本,在当前未解析的rvest issue #175, comment #2中有一个解决方法:
此问题的简化版本:
doc <- read_html('<html><p class="pp">First Line<br />Second Line</p>')
# r2evan's solution:
as.character(rvest::html_node(doc, xpath="//p"))
##[1] "<p class=\"pp\">First Line<br>Second Line</p>"
# rentrop@github's solution, simplified:
innerHTML <- function(x, trim = FALSE, collapse = "\n"){
paste(xml2::xml_find_all(x, ".//text()"), collapse = collapse)
}
innerHTML(doc)
## [1] "First Line\nSecond Line"