Skip to content

Commit b408564

Browse files
My Commits
0 parents  commit b408564

File tree

223 files changed

+27098
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+27098
-0
lines changed

Back-endAPI (Node.js)/app.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const express = require('express');
2+
const axios = require('axios');
3+
const { JSDOM } = require('jsdom');
4+
5+
const app = express();
6+
const PORT = process.env.PORT || 3000;
7+
8+
// Configuration for the /api/scrape endpoint
9+
app.get('/api/scrape', async (req, res) => {
10+
try {
11+
// Check if the query parameter ?keyword is provided
12+
const keyword = req.query.keyword;
13+
if (!keyword) {
14+
return res.status(400).json({ error: 'Missing keyword parameter' });
15+
}
16+
17+
// Amazon search URL with the provided keyword
18+
const url = `https://www.amazon.com/s?k=${keyword}`;
19+
const response = await axios.get(url);
20+
21+
// Create a new JSDOM object with the HTML content from the response
22+
const dom = new JSDOM(response.data);
23+
const document = dom.window.document;
24+
25+
// Array to store product details
26+
const products = [];
27+
28+
// Iterate over each search result item on the page
29+
document.querySelectorAll('.s-result-item').forEach(item => {
30+
// Extract product title
31+
const titleElement = item.querySelector('h2 a');
32+
const title = titleElement ? titleElement.textContent.trim() : 'N/A';
33+
34+
// Extract product rating
35+
const ratingElement = item.querySelector('.a-icon-star .a-icon-alt');
36+
const rating = ratingElement ? parseFloat(ratingElement.textContent.split(' ')[0]) : 0;
37+
38+
// Extract number of reviews
39+
const reviewsElement = item.querySelector('.a-size-base');
40+
const reviews = reviewsElement ? parseInt(reviewsElement.textContent.replace(/[^0-9]/g, '')) : 0;
41+
42+
// Extract product image URL
43+
const imageElement = item.querySelector('.s-image');
44+
const imageURL = imageElement ? imageElement.getAttribute('src') : '';
45+
46+
// Add product details to the array
47+
products.push({ title, rating, reviews, imageURL });
48+
});
49+
50+
// Return product details in JSON format
51+
res.json(products);
52+
} catch (error) {
53+
console.error('Error during scraping:', error);
54+
res.status(500).json({ error: 'Internal server error' });
55+
}
56+
});
57+
58+
// Start the server on the specified port
59+
app.listen(PORT, () => {
60+
console.log(`Server is running on port ${PORT}`);
61+
});

Back-endAPI (Node.js)/node_modules/negotiator/HISTORY.md

Lines changed: 108 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Back-endAPI (Node.js)/node_modules/negotiator/LICENSE

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)