Answer the question
In order to leave comments, you need to log in
How to use browser script with WebAPI on local HTML file using node.js?
I have a code that works in the browser: JSfiddle snippet https://jsfiddle.net/c3tkpsrq - it takes the downloaded html there and turns it into json,
How to run the same code on node.js?
The code uses WebAPI, so if you just copy-paste the code and run it in node.js, then naturally the following code sections will not work:
const dom = document.body;
if (node instanceof HTMLHeadingElement
At the same time, instead of loading html into the html field on JSfiddle, I want to go through this script through several local HTML files, which I access like this:
fs.readdir('output/', (err, files) => {
files.forEach(file => {
if (path.extname(file) === '.html') {
fs.readFile('folder/'+file, 'utf8', function(err, data) {
console.log(data) //выдаёт правильный html, такой же как исходный в сниппете
});
}
})
})
node instanceof HTMLParagraphElement
still doesn't work. Puppeteer also tried, but I get errors that I don’t know how to debug, apparently I’m using it wrong. Answer the question
In order to leave comments, you need to log in
const fs = require('fs');
const path = require('path');
const HTMLParser = require('node-html-parser');
const folderPath = 'html'
fs.readdirSync(folderPath).map(fileName => {
fs.readFile(path.join(folderPath, fileName), 'utf8', function(err, html){
const root = HTMLParser.parse(html);
console.log(root.querySelector('h2').rawText);
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question