Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

>set up express server and scraper

const PORT = 8000;
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');

const app = express();

const articles = [];

app.get('/', (req, res) => {
    res.json('Welcome to my Climate Change News API')
})

app.get('/news', (req, res) => {
    axios.get('https://www.theguardian.com/environment/climate-crisis')
        .then((response) => {
            const html = response.data
            const $ = cheerio.load(html)

            $('a:contains("climate")', html).each(function () {
                const title = $(this).text()
                const url = $(this).attr('href')
                articles.push({
                    title,
                    url
                })
            })
            res.json(articles)
        }).catch((err) => console.log(err))
})

app.listen(PORT, () => console.log(`server running on PORT ${PORT}`));
Comment

PREVIOUS NEXT
Code Example
Javascript :: React ES6 Modules 
Javascript :: card types regex 
Javascript :: JavaScript / jQuery DOM Selectors 
Javascript :: nodejs: Basic: managing file: Read, Write, Create, Delete 
Javascript :: nodejs: http: router simple 
Javascript :: javascript process.env.key with  
Javascript :: remoteevent dont send object 
Javascript :: manter alguns campos objetos javascript 
Javascript :: javascript döngü dizisi 
Javascript :: change y scale phaser 
Javascript :: phaser place items on circle 
Javascript :: phaser add animation event 
Javascript :: phaser play animation after delay 
Javascript :: complex expression in programming 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 
Javascript :: Expresion regular para validar contraseñas 
Javascript :: react three fiber cannon collision 
Javascript :: smembers in redis 
Javascript :: how to run javascript in terminal 
Javascript :: what are array methods in javascript 
Javascript :: select and select based on value in jquery 
Javascript :: js date 
Javascript :: find an object in an array by one of its properties 
Javascript :: Javascript count instances of character in a string 
Javascript :: how to make a delete button in javascript 
Javascript :: name function in javascript 
Javascript :: nodejs debug 
Javascript :: keyboard close when typing react native 
Javascript :: create responsive navbar without javascript 
Javascript :: object methods 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =