1
2import axios from 'axios';
3
4import * as cheerio from 'cheerio';
5
6import { Actor } from 'apify';
7
8
9
10
11
12await Actor.init();
13
14
15const input = await Actor.getInput();
16const { url } = input;
17
18
19const response = await axios.get(url);
20
21
22const $ = cheerio.load(response.data);
23
24 const okButton = $("button:contains('OK')");
25 if (okButton.length > 0) {
26
27 console.log('Clicking OK button');
28
29
30 }
31
32
33
34const heading = [];
35
36 const items = $(".article-item-container.wt-search-result.article-image-carousel");
37 items.each((index, element) => {
38 const obj = {};
39 const id = $(element).find('a').attr('data-article-id');
40 const link = $(element).find('a').attr('href');
41 const titleElement = $(element).find('.text-sm.text-sm-md.text-bold.text-ellipsis').text().trim();
42 const description = $(element).find('.text-sm.text-sm-md.text-ellipsis.m-b-2').text().trim();
43
44 const priceText = $(element).find('.d-flex.justify-content-between.align-items-end.m-b-1 .text-bold').text().trim();
45 const priceMatch = priceText.match(/(\d+(?:,\d{3})*)/);
46 const price = priceMatch ? parseInt(priceMatch[1].replace(/,/g, '')) : null;
47
48 const location = $(element).find('.d-flex.justify-content-between.align-items-end.m-b-1 .text-sm.text-uppercase').text().trim();
49 const currency = $(element).find('.d-flex.justify-content-between.align-items-end.m-b-1 .currency').text().trim();
50
51 obj.id = id;
52 obj.link = "https://www.chrono24.in/"+link;
53 obj.titleElement = titleElement;
54 obj.description = description;
55 obj.price = price;
56 obj.location = location;
57 obj.currency = currency;
58 heading.push(obj);
59 });
60
61
62 for (let i = 0; i < heading.length; i++) {
63 const EachProduct = {};
64 try {
65 const responseItem = await axios.get(heading[i].link);
66 const $item = cheerio.load(responseItem.data);
67
68 const okButtonItem = $item('button:contains("OK")');
69 if (okButtonItem.length > 0) {
70 console.log('OK button found for the second request. Clicking...');
71 }
72
73 const rating = $item('.m-b-2.d-flex.justify-content-between span.rating').text().trim();
74 const maximumImageSize = [];
75 const listOfImages = $item('div[data-zoom-image]');
76
77 listOfImages.each((index, element) => {
78 const imageSmall = $item(element).attr('data-zoom-image');
79 maximumImageSize.push(imageSmall);
80 });
81 EachProduct.rating = rating;
82 EachProduct.images = maximumImageSize;
83 } catch (error) {
84 console.error('Error:', error.message);
85 EachProduct.rating = 'null';
86 EachProduct.imageSmall = "not found";
87 }
88 heading[i].EachProduct = EachProduct;
89 }
90
91
92await Actor.pushData(heading);
93
94
95await Actor.exit();