增加爬虫
This commit is contained in:
75
newBackApi/router/home.js
Normal file
75
newBackApi/router/home.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import express from "express";
|
||||
import Util from '../utils/index.js'
|
||||
import Config from '../config/config.js'
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// 获取首页banner图
|
||||
// http://192.168.31.101:3030/v1/api/home/banner
|
||||
router.get('/banner', async (req, res) => {
|
||||
let $ = await Util.get(`${Config.baseUrl}/?orderby=rand`)
|
||||
let result = []
|
||||
|
||||
$(".new-swiper .swiper-wrapper .swiper-slide").each(function() {
|
||||
let src = $(this).find("span img").attr("src")
|
||||
if (src) {
|
||||
result.push({ src })
|
||||
}
|
||||
})
|
||||
|
||||
res.json({
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
})
|
||||
|
||||
// 获取热门推荐的写真
|
||||
// http://192.168.31.101:3030/v1/api/home/hot
|
||||
router.get('/hot', async (req, res) => {
|
||||
let $ = await Util.get(`${Config.baseUrl}/?orderby=rand`)
|
||||
let result = []
|
||||
|
||||
$(".box-body .tab-content .posts-mini").each(function() {
|
||||
result.push({
|
||||
id: Util.split($(this).find(".item-thumbnail a").attr("href")).replace(/[^\d]/g, ""),
|
||||
thumbnail: $(this).find(".item-thumbnail a img").attr("data-src"),
|
||||
title: $(this).find(".item-heading a").text(),
|
||||
time: $(this).find(".item-meta .icon-circle").text(),
|
||||
view: $(this).find(".item-meta .meta-right .meta-view").text()
|
||||
})
|
||||
})
|
||||
|
||||
res.json({
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
// 获取首页 默认的随机数据
|
||||
// http://192.168.31.101:3030/v1/api/home/list
|
||||
router.get('/list', async (req, res) => {
|
||||
let $ = await Util.get(`${Config.baseUrl}/?orderby=rand`)
|
||||
let result = []
|
||||
|
||||
$(".tab-content .posts-row .posts-item").each(function() {
|
||||
result.push({
|
||||
id: Util.split($(this).find(".item-thumbnail a").attr("href")).replace(/[^\d]/g, ""),
|
||||
thumbnail: $(this).find(".item-thumbnail a img").attr("data-src"),
|
||||
title: $(this).find(".item-heading a").text(),
|
||||
time: $(this).find(".item-meta .icon-circle").text(),
|
||||
view: $(this).find(".item-meta .meta-right .meta-view").text()
|
||||
})
|
||||
})
|
||||
|
||||
res.json({
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
export default router
|
||||
@@ -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
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
@@ -1,20 +1,70 @@
|
||||
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/search/hot
|
||||
// 搜索页面的热门搜索接口
|
||||
// http://192.168.31.101:3030/v1/api/search/hot
|
||||
router.get('/hot', async (req, res) => {
|
||||
const { browser, page } = await crawlApp.page(Config.baseUrl);
|
||||
// await page.waitForSelector('.search-input')
|
||||
let $ = await Util.get(`${Config.baseUrl}/wp-admin/admin-ajax.php?action=search_box`)
|
||||
let result = {
|
||||
hotKeyword: [],
|
||||
recommend: []
|
||||
}
|
||||
|
||||
$(".search-input .search-keywords").eq(0).find("div a").each(function() {
|
||||
result.hotKeyword.push($(this).text())
|
||||
})
|
||||
|
||||
$(".search-input .relates-thumb .swiper-slide a").each(function() {
|
||||
result.recommend.push({
|
||||
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: 200,
|
||||
data: await page.$$eval('.search-keywords', el => el.map(x => { return { title: x } }))
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
// 搜索页面的热门搜索接口
|
||||
// http://192.168.31.101:3030/v1/api/search?keyword=杨晨晨
|
||||
router.get('/', async (req, res) => {
|
||||
let { keyword, page } = req.query
|
||||
page = page || 1
|
||||
|
||||
let $ = await Util.get(`${Config.baseUrl}/page/${page}?s=${encodeURI(keyword)}&type=post`)
|
||||
let result = {
|
||||
currentPage: page,
|
||||
totalPage: $(".pagenav a").eq(2).text(),
|
||||
message: $(".search-content .badg .focus-color").text(),
|
||||
list: [],
|
||||
}
|
||||
|
||||
$(".search-content .posts-item").each(function() {
|
||||
result.list.push({
|
||||
id: Util.split($(this).find(".item-thumbnail a").attr("href")).replace(/[^\d]/g, ""),
|
||||
thumbnail: $(this).find(".item-thumbnail a img").attr("data-src"),
|
||||
title: $(this).find(".item-body .item-heading a").text(),
|
||||
time: $(this).find(".item-body .item-meta .icon-circle").text(),
|
||||
view: $(this).find(".item-body .item-meta .meta-right .meta-view").text()
|
||||
})
|
||||
})
|
||||
|
||||
res.json({
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
export default router
|
||||
@@ -1,60 +1,61 @@
|
||||
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/tag/list
|
||||
// 获取 热门标签
|
||||
// http://192.168.31.101:3030/v1/api/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 $ = await Util.get(Config.hot_tags())
|
||||
let result = []
|
||||
|
||||
for (const handle of categoriesList) {
|
||||
$(".categorieslist ul li a").each(function (i) {
|
||||
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)
|
||||
})
|
||||
}
|
||||
title: $(this).find("h2").text(),
|
||||
count: $(this).find("small p").text().replace(/[^\d]/g, ""),
|
||||
url: Util.split($(this).attr("href")),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
res.json({
|
||||
code: 300,
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
|
||||
// 关闭浏览器
|
||||
// browser.close()
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
// http://192.168.31.101:3000/api/v1/tag/info?url=/xiuren&pages=1
|
||||
// 进入对应标签的列表页面
|
||||
// http://192.168.31.101:3030/v1/api/tag/info?url=/xiuren&page=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 = []
|
||||
let { url, page } = req.query
|
||||
page = page || 1
|
||||
|
||||
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)
|
||||
})
|
||||
let $ = await Util.get(`${Config.baseUrl}${url}/page/${page}`)
|
||||
let result = {
|
||||
count: $(".content-layout .title-h-left .icon-spot").text().replace(/[^\d]/g, ""),
|
||||
introduce: $(".content-layout .muted-2-color").text(),
|
||||
totalPage: $(".pagenav a").eq(2).text(),
|
||||
currentPage: page,
|
||||
list: []
|
||||
}
|
||||
|
||||
$(".content-layout .posts-row .posts-item").each(function (i) {
|
||||
result.list.push({
|
||||
id: Util.split($(this).find(".item-thumbnail a").attr("href")).replace(/[^\d]/g, ""),
|
||||
thumbnail: $(this).find(".item-thumbnail a img").attr("data-src"),
|
||||
title: $(this).find(".item-body .item-heading a").text(),
|
||||
time: $(this).find(".item-body .item-meta .icon-circle").text(),
|
||||
view: $(this).find(".item-body .item-meta .meta-right .meta-view").text()
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
res.json({
|
||||
code: 300,
|
||||
status: 200,
|
||||
copyright: `bmy ${new Date()}`,
|
||||
data: result
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user