marcomontalbano/html-miner

text only? img and a tags not supported?

mark134340 opened this issue · 1 comments

text only? img and a tags not supported?

Hi, the question is not so clear but I try to give you an answer.

You can fetch only the text by providing just a selector.

htmlMiner(html, '.title');

In this way you are getting the .text() from the element. This means that if you provide an img as selector you will get an empty string.


You can get the image src using a function.

htmlMiner(html, function(arg) {
  return arg.$('img').attr('src')
})

Or you can get all src from all images as array

htmlMiner(html, function(arg) {
  return Array.from(arg.$('img')).map(function(img) {
    return arg.$(img).attr('src')
  })
})

You can do the same with an a tag.

htmlMiner(html, function(arg) {
  return arg.$('a').attr('href')
})

If you need to fetch more information from selectors you can simply use an object like this:

htmlMiner(html, {
  _each_: 'a',
  text: function(arg) {
    return arg.$scope.text();
  },
  href: function(arg) {
    return arg.$scope.attr('href');
  }
})

Let me know if this respond to you question so I can close the issue