-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.js
46 lines (39 loc) · 1.58 KB
/
crawler.js
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
const axios = require('axios');
const cheerio = require('cheerio');
// Function to scrape prices from PharmEasy
const searchPharmEasyPrices = async (medicineName) => {
try {
const url = `https://pharmeasy.in/search?name=${encodeURIComponent(medicineName)}`;
console.log(`Fetching URL for PharmEasy: ${url}`);
const { data } = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
},
});
const $ = cheerio.load(data);
const prices = [];
// Scrape the product containers
$('.product-card').each((index, element) => {
const product = $(element).find('.product-card-details .product-title').text().trim();
const price = $(element).find('.product-card-price').text().trim();
if (product && price) {
prices.push({ name: product, price: price });
}
});
return prices;
} catch (error) {
console.error('Error fetching prices from PharmEasy:', error.message);
throw error;
}
};
// Main function to search prices
const searchMedicinePrices = async (medicineName) => {
try {
const pricesPharmEasy = await searchPharmEasyPrices(medicineName);
console.log('Prices from PharmEasy:', pricesPharmEasy);
} catch (error) {
console.error('Error:', error.message);
}
};
// Example usage
searchMedicinePrices('paracetamol'); // Replace 'paracetamol' with any medication name