gpt4 book ai didi

r - 将html结果保存到txt或html文件中

转载 作者:行者123 更新时间:2023-12-04 11:03:33 26 4
gpt4 key购买 nike

我有一个带有html代码的变量。

这是R控制台中的代码变量outpout:

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

我尝试将内容保存到txt文件中
 write.table(code, file='C:/Users/Desktop/code_result.txt')

但它会堆叠此错误:
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
c("cannot coerce class \"c(\"HTMLInternalDocument\", \"HTMLInternalDocument\", \"XMLInternalDocument\", \" to a data.frame", "cannot coerce class \"\"XMLAbstractDocument\")\" to a data.frame")

最佳答案

这是一个简单的解决方案(尽管我更喜欢按照问题注释中的建议使用包XML):

code <- "<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>"

注意:如果您已经有一个包含上面的文本的对象,
您可以将其转换为字符串,因此适用以下代码:
code <- paste(as.character(code), collapse = "\n")

write.table(code,
file='C:/Users/dsargsy/Desktop/code_result.html',
quote = FALSE,
col.names = FALSE,
row.names = FALSE)

关于r - 将html结果保存到txt或html文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com