calebsander / html-soup Goto Github PK
View Code? Open in Web Editor NEWA Node.JS package to do some basic HTML parsing and CSS selection
A Node.JS package to do some basic HTML parsing and CSS selection
Sadly, one of the sites has an element which I unfortunately need to target with #page-submission table table table tr:nth-child(2)
. As such, I'd appreciate support for nth-child
as well :). I might have to dig in to fix it, though I'm not familiar with Typescript (had a bad experience, dug into the philosophy, realized it didn't give runtime checks, and moved on with life).
More of a "nice-to-have" - I'd written input[name="validate_token"]
and it took me a few attempts to figure out why it wasn't matching. Now using [name=validate_token]
, but it might be simple to support :).
This will fix htmlSoup.select(htmlSoup.parse('<a/><a/><b/>'), ':root ~ :last-child')
returning <b/>
twice.
I used something like this to get the attributes I needed (getInnerText redacted as it's currently returning wrong values):
function proxifyNode(node) {
let wrappedChildren = false;
return new Proxy(node, {
get: function(target, property, receiver) {
// children must be wrapped!
if (property === 'children' && !wrappedChildren) {
wrappedChildren = true;
target.children = target.children.map(child => {
if (child.constructor.name === 'HtmlTag')
return proxifyNode(child);
return child;
});
}
// prefer normal properties first
if (property in target)
return target[property];
if (property === 'innerText')
return getInnerText(target);
if (property === 'dataset') {
const dataset = new Proxy(target, {
get: function(target, property, receiver) {
return target.attributes['data-' + property.replace(/([A-Z])/g, (match, p1) => '-' + p1.toLowerCase())];
}
});
// set as property so I only build the dataset Proxy once
target[property] = dataset;
return dataset;
}
// check for attributes after the special properties
if (property in target.attributes)
return target.attributes[property];
},
});
}
And call it as:
const dom = proxifyNode(htmlSoup.parse(body));
Having support for this on the library level would be great. My usecase - I'm sharing scraping code between the server (node.js) and a browser extension - I've also wrapped htmlSoup.select
to behave like document.querySelector
and document.querySelectorAll
as my scrapper code has been using those two functions.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.