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?