如何将 HTML R 对象转换为字符?

var*_*ble 2 xml r httr rvest

这是我的可重现示例:

library(rvest)
page <- html("http://google.com")
class(page)
page
> as.character(page)
Error in as.vector(x, "character") : 
  cannot coerce type 'externalptr' to vector of type 'character'
Run Code Online (Sandbox Code Playgroud)

如何将页面从 html 类转换为字符向量,以便将其存储在某处?

html_text 或 html_attr 之类的 html 函数没有给我完整的源代码。我想存储它,以便以后可以使用 html() 重新加载它。

谢谢。

Dom*_*ois 6

直接保存到文本文件:

capture.output(page, file="file.html")
Run Code Online (Sandbox Code Playgroud)

存储为字符串:

htmltxt <- paste(capture.output(page, file=NULL), collapse="\n")
Run Code Online (Sandbox Code Playgroud)