嗨,我正在尝试为足球比赛建立一个奇怪的展示网站
我试图从betfair获取数据,然后正常工作,然后将其插入我的数据库
问题是该功能仅插入第一项,我该怎么办?
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const util = require('util');
const appendFile = util.promisify(fs.appendFile);
const async = require("async");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'betfair'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var url = 'https://www.betfair.com/sport/football';
var customHeaderRequest = request.defaults({
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}
})
customHeaderRequest.get(url, async function(err, resp, body){
try {
let $ = cheerio.load(body);
let links = $('.section-list .section:nth-child(2) .event-list li').toArray();
for (let link of links) {
const id = $(link).find('.event-information .avb-col-markets a:nth-child(1)').attr('href');
const idx = id.replace('/sport/football/event?eventId=', '');
const home = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(1)').text().trim();
const away = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(2)').text().trim();
const x1 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const x = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
const x2 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(3) a .ui-runner-price').text().trim();
const over = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const under = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
if (home && away) {
connection.query("INSERT INTO bf (id, home, away, x1, x, x2, over, under) VALUES ('"+idx+"', '"+home+"', '"+away+"', '"+x1+"', '"+x+"', '"+x2+"', '"+over+"', '"+under+"')", function (err, result) {
if (err) throw err;
});
}
}
} catch(e) {
}
});
我已经尝试了很多想法,但是在nodejs中不是很好,我正在寻找您的帮助
谢谢