If you are interested in how to remove jQuery from your project, here is a great resource for how to do the same things jQuery does with vanilla JS: http://youmightnotneedjquery.com
It's a paradoy site making fun of those so attached to frameworks they haven't been keeping up with how regular javascript has improved and that they don't need most of the libraries they think they do.
It’s really amazing what regular JS can do - but!!!!!! It’s still a pain in the ass dealing with browser support. It’s getting less relevant hopefully, but its nice not thinking about that with JQuery.
JQuery can lead to some bad code on large sites, but it isn’t such a bad thing as it’s made out to be. Perhaps it’s ubiquity pisses some people off.
Cheerio is jQuery. Here is the description on their github:
Fast, flexible, and lean implementation of core jQuery designed specifically for the server.
Cheerio is great for web scraping and it's usually my go to if I need to scrape statically generated pages, because the DOM API doesn't exist on Node.js.
Right. So I should stick with cheerio then? I figure you could possible run vanilla JS like in a browser to do it without cheerio if the code ran on something like PhantomJS? Then again, that'd probably need some other layer to communicate with Node if you want to do node specific stuff like save the scraped output to a file on disk... sooo cheerio?
There's nothing wrong with cheerio. The arguments here about jQuery have to do with the frontend, not really with jQuery itself.
I would advise against PhantomJS because it hasn't been supported for a while, but if you need to simulate a browser Google's Puppeteer or NightmareJS are decent options. These aren't really solving the same problem, though. If you're scraping static data and don't need to execute any kind of JS on the page, then stick with cheerio.
So it's more about jquery is bad client side because of it's size? I don't really have use for the vanilla equivalents or jquery on frontend personally as I use react there these days.
But if the frontend argument is largely you probably don't need jquery when you think you might because plain js is good enough now, than not sure how that makes jquery any different in other contexts that it could be used?
Either way, my scraper is working fine as is with cheerio. Cheers for the suggestions, not sure what cheerio is using under the hood but I'm happy :)
jQuery is a client-side technology. You don't need it / can't use it on node.js and shouldn't really worry about it. cheerio uses jQuery because they are actually parsing the HTML received by the server. There's nothing at all wrong with using cheerio's jQuery selectors to scrape HTML -- when people talk about using vanilla JS to phase out jQuery, they mean on their client-side code.
jQuery needs a DOM to operate on. When scraping you’re just getting the raw HTML markup from the server. If you wanted a DOM you would need to parse it first, like the browser would normally do.
According to this article you could do that with JsDom and then operate your jQuery on it. But, I believe, this is more or less what cheerio does internally as well.
Also, don’t miss the point - which is that jQuery just isn’t necessary anymore with modern browsers. It was great for its time. We should ideally use native APIs when possible.
138
u/bmey Feb 13 '19 edited Feb 14 '19
If you are interested in how to remove jQuery from your project, here is a great resource for how to do the same things jQuery does with vanilla JS: http://youmightnotneedjquery.com
Edit: here is one from CSS tricks that calls out some modern examples, too - https://css-tricks.com/now-ever-might-not-need-jquery/