1

I am trying to get all values of element

var request = require('request');
var cheerio = require('cheerio');
var url = "https://www.mismarcadores.com";
request(url, function(err, resp, body) {

    if (err) throw err;
    var $ = cheerio.load(body);
    var addUrl= [];
    $('a').each(function (i, element) {
        var a = $(this);
        var href = a.attr('href');
        addUrl.push(href);      
    })
    console.log(array[0]);
})

I have this code that add the links to array called addUrl , this works perfect but now I am looking how to add to this array if the url contains the word 'baloncesto' in href.

Good example : https://www.mismarcadores.com/baloncesto/alemania/

This URL , is good but

Bad example : https://www.mismarcadores.com/golf/

This is wrong.

I am developing this using NodeJS but this is only a simply javascript that now I don't know how to made this.

Could anyone help to me?

Luis Deras
  • 1,239
  • 2
  • 19
  • 48
pepe
  • 335
  • 2
  • 11
  • `var url = "https://www.mismarcadores.com/baloncesto"`? Not really sure what you're asking. Are you looking specifically in that parent folder, or are you allowed subfolders of the same name as well? – Obsidian Age Apr 04 '18 at 22:28
  • Yes I am interest to looking in the parent folder , because one day is basketball but another day could be another sport. – pepe Apr 04 '18 at 22:30

4 Answers4

1

Please try like this:

var request = require('request');
var cheerio = require('cheerio');
var url = "https://www.mismarcadores.com";
var filterStr = 'baloncesto';
request(url, function(err, resp, body) {

    if (err) throw err;
    var $ = cheerio.load(body);
    var addUrl= [];
    $('a').each(function (i, element) {
        var href = $(this).attr('href');
        if (href.includes(filterStr)) addUrl.push(href);
    })
    console.log(addUrl);
})
Songgen
  • 57
  • 1
  • 5
0
you can try this
if(href.contains('baloncesto')){
  addUrl.push(href);      
 }
0

Okay, so what you're looking for is a pattern match - if href contains the string baloncesto. I suspect that this SO thread will be helpful to you - you're basically looking to nest this line -

addUrl.push(href);

- in an if statement, like

if(href.includes('baloncesto')) { addUrl.push(href); }

...but definitely look at that other answer for reference, in case you're using a version of JS that's older and not compatible with ES6.

ingernet
  • 1,342
  • 2
  • 12
  • 29
0

Try this

var request = require('request');
var cheerio = require('cheerio');
var url = "https://www.mismarcadores.com";
request(url, function(err, resp, body) {

if (err) throw err;
var $ = cheerio.load(body);
var addUrl= [];
$('a').each(function (i, element) {
    var a = $(this);
    var href = a.attr('href');
    if (decodeURIComponent(href).contains('baloncesto')){
        addUrl.push(href);    
    } else {
    //do something else
    }
})
console.log(array[0]);

})

LSTM
  • 128
  • 8