源代码:
// 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