frfr
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const path = require('path');
const app = express();
const port = 3000;
// Statische Dateien aus dem 'public'-Ordner bereitstellen
app.use(express.static('public'));
// Route für das HTML-Frontend
app.get('/', (req, res) => {
res.send(`
Webseiten-Link-Extractor
`);
});
// Route zum Extrahieren der Links
app.get('/extract-links', async (req, res) => {
const url = req.query.url;
if (!url) {
return res.status(400).json({ error: 'Keine URL angegeben' });
}
try {
// Lade die Webseite mit Axios
const { data } = await axios.get(url);
// Parse die HTML-Daten mit Cheerio
const $ = cheerio.load(data);
const links = [];
// Extrahiere alle Tags und deren href-Attribute
$('a').each((i, element) => {
const link = $(element).attr('href');
if (link) {
links.push(link);
}
});
// Sende die gefundenen Links als JSON zurück
res.json({ links });
} catch (error) {
res.status(500).json({ error: 'Fehler beim Abrufen der Webseite' });
}
});
// Starte den Server
app.listen(port, () => {
console.log(`Server läuft auf http://localhost:${port}`);
});
Kommentare
Kommentar veröffentlichen