Okay, so basically I have a web scraper, and I save the web scraper information into a local database called "db.json"
I was wondering at how about to tackle into implementing it and having the information display in the view. I'm used to working in like PSQL, so doing this is different for me.. I appreciate the help and I plan on setting up like cron jobs and stuff like that as well but I'll do that later on.
The current partial-jobs maps through out PSQL database and grabs example information right now so it will be changed..
I was also having trouble getting it to the grab the jobs title for more.. organized json but for exmaple
job: jobs.title
which I figured would get the jobs title didn't get anything is it because I have it outside of the function?
Here is the code :
The pushing of the information to the DB is located at the bottom of the searchJobs function.
webScraper.js :
debug = require("../models/conn");
const puppeteer = require("puppeteer");
const axios = require("axios");
const cheerio = require("cheerio");
const db = require("../public/scripts/db")
async function searchJobs(i) {
const url = await axios
.get("https://indeed.com/jobs?q=Web+Developer&l=Atlanta&fromage=last")
.then(response => response)
.then(res => {
const jobs = [];
const $ = cheerio.load(res.data);
$(".result").each((index, element) => {
const title = $(element)
.children(".title")
.text();
const linkToJob = $(element)
.children(".title")
.children("a")
.attr("href");
const body = $(element)
.children(".summary")
.text();
jobs[index] = { title, linkToJob, body };
});
console.log(jobs);
// Push jobs to JSON DB
db.get('jobs').push({
job: jobs
}).write();
return jobs;
});
return url;
}
This is the script that writes the information to a json file.
db.js :
low = require("lowdb"),
FileSync = require("lowdb/adapters/FileSync");
const adapter = new FileSync('db.json')
const db = low(adapter)
db.defaults({ jobs: [], body: []})
.write()
module.exports = db;
module.exports = searchJobs;
This is the jobs route
jobs.js :
const express = require("express"),
router = express.Router();
jobModel = require("../models/jobModel");
// gets job page
router.get("/", async function(req, res) {
const jobData = await jobModel.getAllJobs();
console.log(jobData);
res.render("template", {
locals: {
title: "jobs",
jobData: jobData
},
partials: {
partial: "partial-jobs"
}
});
});
module.exports = router;