Created
January 15, 2023 17:26
-
-
Save matefs/f804e5604926551e6591707e78ae0e44 to your computer and use it in GitHub Desktop.
Simple Nodejs webscrapper with axios express and cheerio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const axios = require('axios'); | |
const cheerio = require('cheerio'); | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.send('Hello World!'); | |
}); | |
app.get('/scrape', (req, res) => { | |
axios.get('https://www.reddit.com/r/webdev/') | |
.then(response => { | |
const $ = cheerio.load(response.data); | |
const titles = []; | |
$('p.title').each((i, elem) => { | |
titles[i] = $(elem).text(); | |
}); | |
res.send(titles); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
}); | |
app.listen(3000, () => { | |
console.log('Example app listening on port 3000!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment