增加爬虫

This commit is contained in:
编码猿
2024-11-22 22:59:41 +08:00
parent 8fdd65b9bb
commit 4bc03fc814
10 changed files with 502 additions and 1573 deletions

View File

@@ -1,42 +1,41 @@
import express from "express";
// import app from "./utils/app.js"
import { Crawl } from '../utils/crawl.js'
import Util from '../utils/index.js'
import Config from '../config/config.js'
const router = express.Router();
const crawlApp = new Crawl()
// http://192.168.31.101:3000/api/v1/info/list?id=6808
// 进入详情的接口
// http://192.168.31.101:3030/v1/api/info/list?id=6808
router.get('/list', async (req, res) => {
let { id } = req.query
const { browser, page } = await crawlApp.page(`${Config.baseUrl}/${id}.html`);
await page.waitForSelector('.article')
let $ = await Util.get(`${Config.baseUrl}/${id}.html`)
let result = {
title: await page.$eval('.article-header h1 a', el => el.innerText),
src_list: await page.$$eval('.article-content .theme-box p img', el => el.map(x => { return { title: x.getAttribute("alt"), alt: x.getAttribute("data-src")} })),
recommend: []
title: $(".article .article-header .article-title a").text(),
src_list: [],
recommend: [],
}
const recommendList = await page.$$('.zib-widget .swiper-wrapper .swiper-slide')
$(".article-content .wp-posts-content p img").map(function(i, el) {
result.src_list.push($(el).attr("data-src"))
})
for (const handle of recommendList) {
$(".swiper-container .swiper-slide a").each(function(i, el) {
result.recommend.push({
id: await handle.$eval('a', el => el.pathname.replace(/[^\d]/g, "")),
thumbnail: await handle.$eval('a img', el => el.getAttribute("data-src")),
title: await handle.$eval('a img+div', el => el.innerText),
time: await handle.$eval('a .px12 item', el => el.innerText),
view: await handle.$eval('a .px12 .pull-right', el => el.innerText),
id: Util.split($(this).attr("href")).replace(/[^\d]/g, ""),
thumbnail: $(this).find("img").attr("data-src"),
title: $(this).find(".text-ellipsis").text(),
time: $(this).find(".px12 item").eq(0).text(),
view: $(this).find(".px12 item").eq(1).text(),
})
}
})
res.json({
code: 300,
status: 200,
copyright: `bmy ${new Date()}`,
data: result
})
})