【发布时间】:2018-11-07 06:30:10
【问题描述】:
我正在尝试在 betfair 上搜索今天的比赛,并希望获得:
主队
客队
x 奇数
画奇数
奇怪
问题是我不断收到多个空格,我尝试了很多但无法修复它,问题不在于修剪,而是导致空行的执行流程
谁能告诉我我做错了什么?
我的代码:
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
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, function(err, resp, body){
$ = cheerio.load(body);
links = $('.section-list .section:nth-child(2) .event-list li');
$(links).each(function(i, link){
var home = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(1)');
var h = home.text();
if(h != null || h!=''){
fs.appendFile('message.txt', h+'\n', function (err) {});
}
});
});
【问题讨论】:
标签: node.js web-scraping cheerio