Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/ac1b02dad3b3fb3be60c7c07093e00f4 to your computer and use it in GitHub Desktop.
Save code-boxx/ac1b02dad3b3fb3be60c7c07093e00f4 to your computer and use it in GitHub Desktop.
NodeJS Web Scraper

NODEJS WEB SCRAPER

https://code-boxx.com/simple-nodejs-web-scraper/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Save below image as basketball.png.
    • Install required modules - npm i jsdom
    • Start the dummy HTTP server - node 1B-server.js
  2. Open your web browser. Access http://localhost/, make sure the server is running.
  3. Open another command terminal, run node 2-scrape.js to scrape the dummy HTML page.

IMAGES

sandwich

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Dummy Product Page</title>
<style>
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#product {
width: 300px;
padding: 10px;
border: 1px solid #eee;
background: #f5f5f5;
}
#pImg {
display: block;
width: 100%;
margin-bottom: 10px;
}
#pName {
font-size: 18px;
font-weight: 700;
}
#pPrice {
font-weight: 700;
color: #f34242;
}
#pDesc {
font-size: 14px;
color: #5c5c5c;
}
#pAdd {
width: 100%;
margin-top: 10px;
border: 0;
padding: 10px;
color: #fff;
background: #b72b2b;
cursor: pointer;
}
</style>
</head>
<body>
<div id="product">
<img src="sandwich.png" id="pImg">
<div id="pName">Sandwich</div>
<div id="pPrice">$12.34</div>
<div id="pDesc">Just a regular SAND wich.</div>
<input type="button" value="Add To Cart" id="pAdd">
</div>
</body>
</html>
// (A) LOAD MODULES
const http = require("http"),
fs = require("fs");
// (B) HTTP SERVER
http.createServer((req, res) => {
switch (req.url) {
// (B1) NOT FOUND
default:
res.writeHead(404);
res.end("NOT FOUND");
break;
// (B2) DUMMY PAGE + IMAGE
case "/":
res.writeHead(200);
res.end(fs.readFileSync("1A-dummy.html"));
break;
case "/sandwich.png":
res.writeHead(200);
res.end(fs.readFileSync("sandwich.png"));
break;
}
}).listen(80);
console.log("HTTP SERVER IS RUNNING");
// (A) LOAD JSDOM
// npm i jsdom
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
// (B) FETCH
fetch("http://localhost/")
.then(res => res.text())
.then(txt => {
// (B1) PARSE HTML
const dom = new JSDOM(txt);
const doc = dom.window.document;
// (B2) EXTRACT INFORMATION
// console.log(doc);
console.log(doc.getElementById("pName").innerHTML);
console.log(doc.getElementById("pPrice").innerHTML);
console.log(doc.getElementById("pDesc").innerHTML);
console.log(doc.getElementById("pImg").src);
})
.catch(err => console.log(err));
curl https://user-images.githubusercontent.com/11156244/281624730-68dcbe1f-5aff-42e2-b917-7f890a1b2d1f.png --ssl-no-revoke --output sandwich.png
call npm i jsdom
node 1B-server.js
curl https://user-images.githubusercontent.com/11156244/281624730-68dcbe1f-5aff-42e2-b917-7f890a1b2d1f.png --ssl-no-revoke --output ./sandwich.png
source "npm i jsdom"
node ./1B-server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment