I am trying to retrieve meta data from a website that the user types into a form on my Wix site.
I fetch the HTML successfully but as soon as I add Cheerio or htmlparser2, i get the same error:
here is my exact code:
$w.onReady(function () {
$w("#button2").onClick(() => {
console.log('clicked GO')
scrape(url)
})
let url
$w("#input4").onKeyPress((event) => {
url = $w("#input4").value
console.log(url)
if (event.key === "Enter") {
console.log('enter')
scrape(url)
}
});
});
function scrape(url) {
fetch(url).then(response => {
return response.text()
}).then(html => {
// const cheerio = require('cheerio')
// let doc = cheerio.load(html)
const htmlparse = require('htmlparser2');
let doc = htmlparse.parseDocument(html)
// let title = 'test title'
let title =
doc("title").text() ||
doc('meta[property="title"]').attr("content") ||
doc('meta[property="og:title"]').attr("content") ||
"_no_title_found";
$w("#text15").text = title + " | Length: " + title.length
}).catch(function (err) {
console.warn('err: ', err);
});
}
I have tried with both import and require.
I have tried putting the require outside the onReady block, just inside the onReady block, and just before the actual call (as shown).
I have definitely installed the cheerio and htmlparser2 packages
- Is this even possible on to do in the front end?
- Is there another way to parse HTML?
- Why does the error refer to htmlparser2 even when I use cheerio?