How to add a date to an item thats is scraped with cheerio

How to add a date to an item thats is scraped with cheerio
0

#1

I have created a small script that will scrape data of football matches. From the website I will scrape the matchdate, time, home team and away team.

The script I have created:

var cheerio = require('cheerio');
var request = require('request');

request('https://www.fotmob.com/leagues/47/matches/', function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);

    $('.fm-fixture').each(function(i, element){

      var a = $(this);
      var homeTeam = a.children().eq(1).children().text();
      var awayTeam = a.children().eq(3).children().text();
      var time = a.children().eq(2).children().text();
      var day = a.prev().text();

      var metadata = {
        homeTeam: homeTeam,
        awayTeam: awayTeam,
        time: time,
        day: day
      };
      console.log(metadata);
    });

  }
});

The output now:

{ homeTeam: 'Burnley',
  awayTeam: 'AFC Bournemouth',
  time: ' 16:00',
  day: 'May 13, 2018  ' }
{ homeTeam: 'Crystal Palace',
  awayTeam: 'West Bromwich Albion',
  time: ' 16:00',
  day: '' }
{ homeTeam: 'Huddersfield Town',
  awayTeam: 'Arsenal',
  time: ' 16:00',
  day: '' }

But the problem is that on the website per date all the matches from that day are displayed. So only the first one scraped will get the date from that day. How can I add to the other matches also the date May 13, 2018 for example?


#2

You can loop through the data and add/update date from the first one.

data.foreach(function(item, index){
if(!item.day) data[index].day = data[0].day
})

#3

You do know there are APIs available with this info?


#4

I’m testing the cheerio module and not going to use this data. I want to use an api to get football.
Which apis do you recommend? There are a lot of apis with football data.


#5

I signed up for sport radar. Haven’t tried any others but I know there are others out there.