Files
ClassContent/历届学生/逯帅帅/项目开发/上课项目/爬虫/index.js
2024-09-27 02:06:13 +08:00

26 lines
739 B
JavaScript

const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const app = express()
const port = 3000
const config = require("./config");
app.get('/', async (req, res) => {
var result = await axios.get(config.url)
var $ = cheerio.load(result.data);
var list = [];
$("#newscontent ul li").each(function (index,value) {
list.push({
type: $(this).find(".s1").text(),
title: $(this).find(".s2 a").text(),
news: $(this).find(".s3 a").text(),
author: $(this).find(".s4").text(),
url: config.url+ $(this).find(".s3 a").attr("href"),
})
});
res.json(list)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})