# Inserting HTML snippet into an HTML file using node.js

46
November 24, 2020, at 7:10 PM

I'm doing some webscraping using puppeteer and want to store the HTML snippets I scrape inside an existing file.

The snippets I scrape will look something like this:

<div class="question" style="padding-left: 20px; padding-right: 20px;">
<p>Formulate the equation for the reaction of H<sub>2</sub>SO<sub>4</sub> with Mg(OH)<sub>2</sub>.</p>
<div class="marks">[1]</div>
<div class="question_part_label">b.</div>
</div>


I then want to insert them into an HTML template file that looks like this:

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>MathJax example</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Lato">
</head>
<body style="font-family: Lato;">

<p>
INSERT HERE

</p>
</body>
</html>


Each snippet would have its own file.

I was thinking of using

fs.copyFileSync() to copy the template file and then writing the snippet to it either by directly placing the correct spot or by appending it and then appending the final 3 tags. I wasn't sure which write file function to use to achieve this. It would be great if you could help me out, thanks.

POPULAR ONLINE

READ ALSO

### Unable to set value using useState()

I'm trying to set value that is being imported from another functionBut when I try to set it using useState() it returns empty array

18

### error when trying to run a code at pycharm and when typing pip at the cmd

I want to start with web development with python, django, react, javascript and when I download nodejs at the windows PowerShell it showed me that its downloading python again and now when I want to run a code at PyCharm it is showing me this error:

20

### Upgrading Hapi and JWT validation to latest version

I've been asked to update an old project to the latest dependencies, and I'm facing some troubles with JWT authentication

30

### my get request does not work but changing it to a post works

I have a get request that should return all of the logged-in user's project they created, I believe the code was written wellwhen I run it on postman I consistently get a 401 unauthorised error, but when I change the request to patch for example, and I also run a patch request...

21