Reputation: 21
I have a html file.
<!DOCTYPE HTML>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<div align="left" style="height: 475px;">
<noscript><div style="color: red; width: 30%; border: 1px solid red; padding: 4px; font-family: sans-serif;"></div></noscript>
<div id="x"></div>
<script type="text/javascript" src="x.js"></script>
<script type="text/javascript">x.embed("x","720px","475px","x.xml","true","false");</script>
</div>
</body>
</html>
I want to insert css script within the <head></head>
and the file become this:
<!DOCTYPE HTML>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css">
body{margin: 0px 0px 0px 0px; background-color:black;}
</style>
</head>
<body>
<div align="left" style="height: 475px;">
<noscript><div style="color: red; width: 30%; border: 1px solid red; padding: 4px; font-family: sans-serif;"></div></noscript>
<div id="x"></div>
<script type="text/javascript" src="x.js"></script>
<script type="text/javascript">x.embed("x","720px","475px","x.xml","true","false");</script>
</div>
</body>
</html>
Can anyone show me the steps how to do that with R? Detailed explanation will be appreciated! Thanks!
Upvotes: 2
Views: 3084
Reputation: 84699
You can do like this:
library(xml2)
h <- as_list(read_html("yourfile.html"))
css <- list('body {background-color:black;}')
attr(css, "type") <- "text/css"
h$html$head$style <- css
write_html(as_xml_document(h$html), "outfile.html",
options=c("format","no_declaration"))
outfile.html:
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title></title>
<style type="text/css">body {background-color:black;}</style>
</head>
Upvotes: 4