Skip to content

Instantly share code, notes, and snippets.

@berdosi
Created March 25, 2018 22:48
Show Gist options
  • Save berdosi/f04a154efafa9e3e1e38a5171d1f3ea5 to your computer and use it in GitHub Desktop.
Save berdosi/f04a154efafa9e3e1e38a5171d1f3ea5 to your computer and use it in GitHub Desktop.
get data from bamosz.hu
const rq = require("http");
const url = "http://www.bamosz.hu/alapoldal?isin=HU0000711353";
const htmlParser = require("htmlparser");
const parseHtml = function(html) {
return new Promise((resolve, reject) => {
(new htmlParser.Parser(
new htmlParser.DefaultHandler(
(error, dom) => {
if (error) reject(error);
else resolve(dom);
}))).parseComplete(html);
})
}
rq.get(url, res => {
const { statusCode } = res;
if (statusCode !== 200) {
console.error("Failed: " + statusCode);
res.resume();
return;
} else {
console.info("status 200");
}
res.setEncoding("utf-8");
let rawData = "";
res.on("data", chunk => rawData += chunk);
res.on("end", () => {
const htmlData = parseHtml(rawData)
.then(html => {
const documentRoot = html.find(tag => tag.name==="html");
const childReducer = function(collection, current) {
// each child may have a children property, with further ancestors - also to be appended to the collection
collection.push(current);
if (current.children) current.children.forEach(childNode => childReducer(collection, childNode))
return collection;
};
const childCollection = documentRoot.children.reduce(childReducer, []);
console.log(
childCollection.filter(child => child.name == "table"));
})
.catch(e => console.error(e));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment