1.0.2 • Published 9 years ago

html-json v1.0.2

Weekly downloads
5
License
ISC
Repository
github
Last release
9 years ago

#html-json

Simple helper to convert HTML content to JSON

#Usage

###Simple query###

  json = require('html-json')(pageContent).extract({
    'title' : title: 'div.container > h1' // by default to get text 
  });
  
  //result 
  {
    'title' : 'Testing'
  }

###Function###

  json = require('html-json')(pageContent).extract({
    author_page_uri: function($body) {
        return $body.find('div.container > div > a').attr('href').trim();
    }
  });
  
  //result 
  {
    'author_page_uri' : '/data?post=1'
  }

###LIST###

  json = require('html-json')(pageContent).extract({
    author_page_uri: function($body) {
      images: {
          'PARENT' : '#thumbnail > div.each_box > div > ul img',
          'LIST' : {
              '348px' : function($image){
                  return $image.attr('src');
              },
              '600px' : function($image){
                  return $image.attr('src').replace('&w=348&h=348', '&w=600&h=600');
              }
          }
      }
    }
  });
  
  //result 
  {
    'images' : [
      {
        '348px' : 'http://image.image/image1.jpg&w=348&h=348'
        '600px' : 'http://image.image/image1.jpg&w=600&h=600'
      },
      {
        '348px' : 'http://image.image/image2.jpg&w=348&h=348'
        '600px' : 'http://image.image/image2.jpg&w=600&h=600'
      }
    ]
  }

###OBJECT###

  json = require('html-json')(pageContent).extract({
    author_page_uri: function($body) {
      'cover': {
        'PARENT' : ' #M201106_0_getTakelook_P00a400040052 > img',
        'OBJECT' : {
            'alt' : function($image){
                return $image.attr('alt');
            },
            'url' : function($image){
                return $image.attr('src');
            } 
        }
      }
    }
  });
  
  //result 
  {
    'cover' : {
      'alt' : 'this is cover',
      'url' : 'http://xxxxxxx'
    }
  }

More example please reference test/test.js

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.0.2

11 years ago

0.0.1

11 years ago