修改爬虫
This commit is contained in:
43
newBackApi/router/info.js
Normal file
43
newBackApi/router/info.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import express from "express";
|
||||
// import app from "./utils/app.js"
|
||||
import { Crawl } from '../utils/crawl.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
|
||||
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 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: []
|
||||
}
|
||||
|
||||
const recommendList = await page.$$('.zib-widget .swiper-wrapper .swiper-slide')
|
||||
|
||||
for (const handle of recommendList) {
|
||||
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),
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 300,
|
||||
data: result
|
||||
})
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user