r/javascript•
oloftw

Having trouble with requireJS and Cheerio, please help!

solved!

I'm extremely new to JS so sorry in advance...

I'm gonna do some web-scraping with cheerio and understood that I need require.js to get that package into my js scripts correctly. So I've read up on how to use require.js and watch a couple tutorials online and ended up with what you can see in my index.html at https://github.com/olofens/skarpeiveckan

The problem I have is that require complains about a module named "parse", which is called in line 5 of cheerio.js, "var parse = require('./parse')," is not loaded.

The cheerio website tells me that I should only need to do require("cheerio"), and as I have understood it require.js should handle nested dependencies and so on.

Any help on this would be appreciated!

Posted Aug 9, 2018, 8:23 AM UTC

04
ChronSyn•

There's a few things to consider here, and more info would be helpful.

Firstly, web-scraping can often only be done from the server-side (in this case, using node.js) as web browsers, being the sandboxed environments that they are, don't often allow cross-domain requests whereas a server can request anything it wants as long as you know how to. In our case, you would use the request library for node to GET the page you require.

Secondly, Cheerio runs in node. It's essentially jquery but runs on the server. There is no benefit to not using jquery on client-side if scraping, as there will be a way to load the scraped HTML into jQuery and then parse.

Finally, is there an API available instead? Most websites don't have one, but I always like to check because it often makes things so much easier to write and CAN be done from the web browser in most cases (there's a few exceptions, but they're uncommon).

So, are you using node.js? If so, don't use requireJS - instead, do everything within your node app. It's a horrible way of doing it and wouldn't be production-level standard, but I gather that this is a small personal project from your post. If you're trying to use the web-browser/client side to scrape, you're limited in what you can do, and you wouldn't use Cheerio in that case.

I've done page scraping in the past, even for some projects used within a business I used to work for, and it's notoriously difficult to get right if pages are constructed well. An advanced example involved scraping a list of 40+ items from a page, each of which required another 6 page requests to gather the information from subpages (240+ requests), and then gathering the data from each into a single object for parsing. In all cases though, I could only do it from the server-side because of limitations of the browser.

If you decide to go down the route of node.js, check into r/node and the community (including myself) will be more than happy to help over there.

3
ThatDanishDude•

Cheerio describes itself as a "Fast, flexible & lean implementation of core jQuery designed specifically for the server. "

I don't think you can make it work on the client side, which is what you're currently trying to do.

2
vue-rocks•

Maybe you need to work on your fundamentals before diving into libraries and such. Get a good feel for JS and web development, then start adding libraries

1
[deleted]•

[deleted]

1
oloftwOP•

u/ChronSyn u/ThatDanishDude u/vue-rocks u/stutterbug

Thanks for the replies! I realize now that cheerio is made for node, something I should have realized when all cheerio tutorials were for node.js... bummer. And also as vue-rocks says, I should get a better grasp of js first. I've written 4 lines and already trying to install modules. Obviously it's not working out. I'm gonna put the web scraping on hold for a bit and tune my website first with js, load it with data manually, and then gather the data from my intended sources via web-scraping. At that point I hope I'm more accustomed to js in general. I'm gonna have a look into node when i get there.

Again, thank you for taking the time!

1