Scraping specific text value from a website

Viewed 25

This is my first post here, hope I dont violate any rules. I am trying to scrape the word "fed" from https://www.marketwatch.com/economy-politics/calendar I can scrape all , but how can I add only get data which has the word "fed" in it?

This is the code so far.

const PORT = 3000
const express = require('express')
const axios = require('axios')
const cheerio = require('cheerio')


const app = express()

const fedTimes = []

app.get('/', (req,res) => {
    res.json('Welcome to Fed Speech Times')
})

app.get('/fed', (req, res) => {
    axios.get('https://www.marketwatch.com/economy-politics/calendar')
    .then((response) => {
        const html = response.data
        const $ = cheerio.load(html)

        $('td', html).each(function() {
            const title = $(this).text()
                fedTimes.push({
                    title
                })
        })
        res.json(fedTimes)
    }).catch((err) => console.log(err))
})

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

Found my on answer. Here is the code. tr:contains(speaks) did the trick for me.

app.get('/stock', (req, res) => {
    axios.get('https://www.marketwatch.com/economy-politics/calendar')
    .then((response) => {
        const html = response.data
        const $ = cheerio.load(html)
        
        $('tr:contains(speaks)', html).each(function() {
            const time = $(this).text()
                fedTimes.push({
                    time
                })
        })
        res.json(fedTimes)
    }).catch((err) => console.log(err))
})
Related