Обновление в выпуске. Теперь у меня есть все в моей базе данных Монго, но на моей странице ничего не отображается. Похоже, что-то делать с bootbox. Если я нажму «Соскрести новые статьи», предыдущий диалог исчезнет, и очистка произойдет успешно. После проверки появляется новый раздел bootbox с пустым тегом h3.
https://github.com/TylerCEdge/tester
В настоящее время я регистрирую все 20 консолей результатов, и все собираются на Монго, но на моей странице ничего не отображается. Вероятно, это орфографическая ошибка, но у меня возникли проблемы с ней.
// Scrape Script
// =============
// Require axios and cheerio
var axios = require("axios");
var cheerio = require("cheerio");
var URL = "https://thehackernews.com/search?"
function scrape(cb) {
// First, we grab the body of the html with axios
axios.get(URL).then(function (response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(response.data);
var articles = [];
// Now, we grab every h2 within an article tag, and do the following:
$("h2").each(function () {
// Save an empty dataToAdd object
var dataToAdd = {};
// Add the text and href of every link, and save them as properties of the dataToAdd object
dataToAdd.title = $(this)
.text()
// Removes unwanted characters from the titles
.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, ' ');
dataToAdd.link = $(this)
.parent().parent().parent().parent()
.find("a")
.attr("href");
dataToAdd.img = $(this)
.parent().parent().parent()
.find("div.home-img").find("div.img-ratio").find("img")
.attr("data-src")
dataToAdd.summary = $(this)
.parent()
.find("div.home-desc")
.text()
.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, ' ')
// console.log(dataToAdd);
articles.push(dataToAdd);
// for (i = 0; i = dataToAdd.length; i++) {
// articles.push(dataToAdd[i]);
// }
});
cb(articles);
console.log(articles);
});
};
module.exports = scrape;
// Server routes
// Import Scrape
var scrape = require("../scripts/scrape");
// Import headlines/notes
var headlinesController = require("../controllers/headlines");
var notesController = require("../controllers/notes");
module.exports = function (router) {
router.get("/", function (req, res) {
res.render("home");
});
router.get("/saved", function (req, res) {
res.render("saved");
});
router.get("/api/fetch", function (req, res) {
headlinesController.fetch(function (err, docs) {
if (!docs || docs.insertedCount === 0) {
res.json({
message: "no new articles today. Check back tomorrow!"
});
}
else {
res.json({
message: "Added " + docs.insertedCount + " new articles!"
});
}
});
});
router.get("/api/headlines", function (req, res) {
var query = {};
if (req.query.saved) {
query = req.query;
}
headlinesController.get(query, function (data) {
res.json(data);
});
});
router.delete("/api/headlines/:id", function (req, res) {
var query = {};
query._id = req.params.id;
headlinesController.delete(query, function (err, data) {
res.json(data);
});
});
router.get("/api/notes/:headline_id?", function (req, res) {
var query = {};
if (req.params.headline_id) {
query._id = req.params.headline_id;
}
notesController.get(query, function (err, data) {
res.json(data);
});
});
router.delete("/api/notes/:id", function (req, res) {
var query = {};
query._id = req.params.id;
notesController.delete(query, function (err, data) {
res.json(data);
});
});
router.post("/api/notes", function (req, res) {
notesController.save(req.body, function (data) {
res.json(data);
});
});
}