修改爬虫

This commit is contained in:
编码猿
2024-11-22 12:31:44 +08:00
parent 07c46ac300
commit 8fdd65b9bb
11 changed files with 2551 additions and 2 deletions

43
newBackApi/router/info.js Normal file
View 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

View File

@@ -0,0 +1,20 @@
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/search/hot
router.get('/hot', async (req, res) => {
const { browser, page } = await crawlApp.page(Config.baseUrl);
// await page.waitForSelector('.search-input')
res.json({
code: 200,
data: await page.$$eval('.search-keywords', el => el.map(x => { return { title: x } }))
})
})
export default router

60
newBackApi/router/tags.js Normal file
View File

@@ -0,0 +1,60 @@
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/tag/list
router.get('/list', async (req, res) => {
const { browser, page } = await crawlApp.page(Config.hot_tags());
await page.waitForSelector('.categorieslist ul')
const categoriesList = await page.$$('.categorieslist ul li')
let result = []
for (const handle of categoriesList) {
result.push({
title: await handle.$eval('h2', el => el.innerText),
count: await handle.$eval('small p', el => el.innerText.replace(/[^\d]/g, "")),
url: await handle.$eval('a', el => el.pathname)
})
}
res.json({
code: 300,
data: result
})
// 关闭浏览器
// browser.close()
})
// http://192.168.31.101:3000/api/v1/tag/info?url=/xiuren&pages=1
router.get('/info', async (req, res) => {
let { url, pages } = req.query
pages = pages || 1
const { browser, page } = await crawlApp.page(`${Config.baseUrl}${url}/page/${pages}`);
await page.waitForSelector('.content-layout .posts-row')
const postsList = await page.$$('.posts-item')
let result = []
for (const handle of postsList) {
result.push({
id: await handle.$eval('.item-thumbnail a', el => el.pathname.replace(/[^\d]/g, "")),
thumbnail: await handle.$eval('.item-thumbnail a img', el => el.getAttribute("data-src")),
title: await handle.$eval('.item-body .item-heading a', el => el.innerText),
tags: await handle.$$eval('.item-body .item-tags a', links => links.map(x => { return { title: x.innerText, href: x.href } })),
time: await handle.$eval('.item-body .item-meta item', el => el.innerText),
view: await handle.$eval('.item-body .item-meta .meta-right item', el => el.innerText)
})
}
res.json({
code: 300,
data: result
})
})
export default router