- Joined
- Jan 31, 2022
- Messages
- 1
- Reaction score
- 0
JSON:
{
"name": "web-scraper",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.4",
"cheerio": "^1.0.0-rc.10",
"cors": "^2.8.5",
"express": "^4.17.1"
}
[QUOTE]
}
Code:
const PORT = 8000
const axios = require('axios')
const express = require('express')
const app = express()
const cors = require('cors')
app.use(cors())
const url = 'https://www.theguardian.com/uk'
app.get('/', function (req, res) {
res.json('This is my webscraper')
})
app.get('/results', (req, res) => {
axios(url)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
const articles = []
$('.fc-item__title', html).each(function () { //<-- cannot be a function expression
const title = $(this).text()
const url = $(this).find('a').attr('href')
articles.push({
title,
url
})
})
res.json(articles)
}).catch(err => console.log(err))
})
app.listen(PORT, () => console.log(`server running on PORT ${PORT}`))