Rooni commited on
Commit
6c4d40b
·
verified ·
1 Parent(s): c926d8b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +39 -21
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
- // Запрос HTML-кода страницы
11
- const response = await axios.get('https://hdrezka180maa.org/animation');
12
- const html = response.data;
13
- const $ = cheerio.load(html);
14
- const parsedResults = [];
15
-
16
- // Парсинг данных с использованием селекторов
17
- $('.b-content__inline_item').each((index, element) => {
18
- const titleElement = $(element).find('.b-content__inline_item-link a');
19
- const title = titleElement.text().trim();
20
- const pageUrl = titleElement.attr('href');
21
- const imageUrl = $(element).find('.b-content__inline_item-cover a img').attr('src');
22
-
23
- parsedResults.push({
24
- title: title,
25
- imageUrl: imageUrl,
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: 'Произошла ошибка сервера при парсинге.' });