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?

2
  • 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? Commented Apr 4, 2018 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. Commented Apr 4, 2018 at 22:30

4 Answers 4

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);
})
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0
you can try this
if(href.contains('baloncesto')){
  addUrl.push(href);      
 }

Comments

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]);

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.