3

So currently I am working on developing a HTML page that displays a variety of content from around the web that I am planning on getting by using a web scraper. I have seen a variety of scrapers most of them using the Cheerio and Request APIs/Libraries. However all of these tutorials(such as:http://www.netinstructions.com/simple-web-scraping-with-node-js-and-javascript/ ) utilize Node.js rather than just a HTML file and .js files. I have no interest in using node.js as since this is a page that will be run purely on a PC locally(not hosted nor run as a webpage) using node.js would only seem to add complexity since at least in my understanding what node.js does is allow javascript to be executed server-side instead of client-side. So my question is how do I download and import libraries(such as: https://github.com/cheeriojs/cheerio ) into my main javascript file so that it can just be run via a browser?

Edit: Even if node.js is not just for server side my question stands. Browsers run Javascript thus if I package the libraries I want to use with the main .js and reference them it will work there without node.js. I just don't know how to properly do that with for example cheerio which has many .js files. Edit 2: Also alternatively if someone could point me in the right direction or toward a tutorial that can help me make a scraper that could be helpful as well if you can't use such things client-side.

Holmes
  • 107
  • 3
  • 9
  • I think your understanding of node.js is a little bit wrong. It is not used to execute code server side, it is used to execute javascript code. Express (which is a node.js library) is an example of a node.js server. – Luple Feb 27 '18 at 23:13
  • Here is one example of scrapping on the client: [link](https://medium.freecodecamp.org/client-side-web-scraping-with-javascript-using-jquery-and-regex-5b57a271cb86) – Kevin Amiranoff Feb 27 '18 at 23:26

2 Answers2

2

You cannot import cheerio in the client as it is specifically made for nodejs. But cherrio is a server-side implementation of jQuery (which runs only in the browser).

To import jquery, you can it as a link in your html. For example :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You should place this file before importing your own javascript file.

Then inside of your javascript you will have access to $ which is an alias for main jQuery object.

Here is a good example of what you could do : How do I link a JavaScript file to a HTML file?

Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90
0

UPDATE: looking for a similar solution found this : Github solution you just install the package with

npm i cheerio-without-node-native@0.20.2

and will be able to use cheerio without nodejs. Hope it helps.

Ac2442
  • 11
  • 1