如何将 HTML R 对象转换为字符? [英] How to convert an HTML R object to character?
本文介绍了如何将 HTML R 对象转换为字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的可重现示例:
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'
如何将页面从 html 类转换为字符向量,以便将其存储在某处?
How can I convert page from an html class to a character vector so I can store it somewhere?
html_text 或 html_attr 之类的 html 函数并没有给我完整的源代码.我想存储它,以便以后可以使用 html() 重新加载它.
The html functions like html_text or html_attr don't give me the whole source. I would like to store it so I can later re-load it with html().
谢谢.
推荐答案
要直接保存到文本文件:
To save directly to a text file:
capture.output(page, file="file.html")
存储为字符串:
htmltxt <- paste(capture.output(page, file=NULL), collapse="\n")
这篇关于如何将 HTML R 对象转换为字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文