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