Grabbing result payload from parser
cyzanfar opened this issue · 2 comments
Hi!
I'd like to access the metadata extracted but the example only shows a console.log(result)
. I'm missing something here...
here is my code:
const parser = new Parser(handler(url), { decodeEntities: true });
const html = await page.content();
parser.write(html);
// somewhere here i'd like to get the parsed data `result`
parser.end();
const handler = (url) => new Handler((err, result) => result, { url });
Please help :)
I solved the issue by doing this:
let res = null
const parser = new Parser(new Handler(
(err, result) => {
res =result;
},
{
url
}
), { decodeEntities: true });
const html = await page.content();
parser.write(html);
parser.end();
It's not pretty...
This won’t really work if you have any async code or if the HTMLParser2 API changes to include async code, I’d recommend getting familiar with callbacks or promises in JavaScript to more easily handle this. You can just wrap this package in a promise, which is what I typically do in other places. Example: https://github.com/blakeembrey/node-scrappy/blob/435746fa180c035a12e9feba0fe2de863d4bba6f/src/plugins/htmlmetaparser.ts#L179-L192. If you wrap it in a promise you can can use async
and await
, but this package won’t provide that for you because it’s meant to be used with HTMLParser2 which only has a callback-based API.