Spaces:
Sleeping
Sleeping
File size: 1,592 Bytes
abed4cc e6227e8 0cb0cc3 40f1235 abed4cc 5d38af1 6c4d40b 0cb0cc3 2bdd039 6c4d40b 0cb0cc3 6c4d40b 2bdd039 0cb0cc3 2bdd039 abed4cc 0cb0cc3 abed4cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const app = express();
app.use(express.json());
const parsePage = async (url) => {
const response = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const parsedResults = [];
$('.b-content__inline_item').each((index, element) => {
const titleElement = $(element).find('.b-content__inline_item-link a');
const title = titleElement.text().trim();
const pageUrl = titleElement.attr('href');
const imageUrl = $(element).find('.b-content__inline_item-cover a img').attr('src');
parsedResults.push({
title: title,
imageUrl: imageUrl,
pageUrl: pageUrl,
});
});
return parsedResults;
};
app.get('/parse', async (req, res) => {
try {
const baseUrl = 'https://hdrezka180maa.org/animation';
let currentPage = 1;
let hasMorePages = true;
const allResults = [];
while (hasMorePages) {
const pageUrl = currentPage === 1 ? baseUrl : `${baseUrl}/page/${currentPage}/`;
const results = await parsePage(pageUrl);
if (results.length === 0) {
hasMorePages = false;
} else {
allResults.push(...results);
currentPage++;
}
}
res.json(allResults);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Произошла ошибка сервера при парсинге.' });
}
});
const port = 7860;
app.listen(port, () => {
console.log(`Сервер запущен на порту ${port}`);
}); |