Having trouble with requireJS and Cheerio, please help!
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
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
requestlibrary 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.