Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript libaries such as Cheerio without node.js [duplicate]

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.

like image 850
Holmes Avatar asked Sep 14 '25 15:09

Holmes


1 Answers

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?

like image 121
Kevin Amiranoff Avatar answered Sep 16 '25 05:09

Kevin Amiranoff