源代码:
// import libraries
const request = require('request');
const cheerio = require('cheerio');
// define url to scrape
const url = 'https://www.flashscore.com/football/france/ligue-1/';
// make http request
request(url, (error, response, html) => {
if(!error && response.statuscode == 200){
// get the html
const $ = cheerio.load(html);
// select the data that we need
const tablerows = $('.event__match');
// loop through the rows
tablerows.each(function(){
const team1name = $(this).find('.event__participant--home .event__participant-name').text();
const team2name = $(this).find('.event__participant--away .event__participant-name').text();
const score = $(this).find('.event__scores .event__scores-value').text();
const result = team1name + ' ' + score + ' ' + team2name;
console.log(result);
});
}
});
数据:
nantes 0 - 0 monaco
lille 2 - 1 dijon
bordeaux 1 - 0 metz
strasbourg 3 - 1 nimes
rennes 3 - 1 lorient
angers 0 - 0 brest
reims 0 - 0 lens
marseille 1 - 0 montpellier
nice 1 - 0 saint-etienne