R - Write a HTML file from URL/HTML Object/HTML Response

旧城冷巷雨未停 提交于 2019-12-11 13:07:07

问题


I want to save a HTML file using a URL from R. I have tried to save the response object(s) after using GET and read_html functions of httr and rvest packages respectively, on the URL of the website, I want to save the HTML of. But that didn't work out to save the actual contents of the website.

url = "https://facebook.com"
get_object = httr::GET(url); save(get_object, "file.html")
html_object = rvest::read_html(url); save(html_object, "file.html")

Neither of these work to save the correct output (i.e, the HTML content of the webpage in a .html file) of the actual website in the HTML file.


回答1:


Use str(object) to figure out what you are working with. In both cases, you were trying to write non-text to a text file.

Here's how to get the text and write it using both of your libraries...

url = "https://facebook.com"

library(httr)
get_object = GET(url)
cat(content(get_object, "text"), file="temp.html")

library(rvest)
html_object = read_html(url)
write_xml(html_object, file="temp.html")


来源:https://stackoverflow.com/questions/37631226/r-write-a-html-file-from-url-html-object-html-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!