Git Product home page Git Product logo

muninn's Introduction

Muninn

npm Build Status License

Muninn is a fast and flexible HTML parsing tool that simplifies the process of extracting data from HTMLs. It allows you to create a configuration file, making it easy to keep parser settings up to date even when selectors change. With its easy-to-learn syntax and the power of the cheerio library for parsing, Muninn is a versatile solution for various parsing needs.

Features

  • Fast and efficient HTML parsing
  • Easy-to-learn syntax for creating parser configurations
  • Flexibility to handle changing selectors
  • Powered by the popular cheerio library


Sample

import { parse } from 'muninn';

const config = {
  schema: {
    title: '#productTitle',
    price: '#priceblock_ourprice',
    rating: {
      selector: '#acrPopover span | float',
      regex: /\d+\.?\d?/
    },
    features: {
      selector: '#productOverview_feature_div tr.a-spacing-small | array',
      schema: {
        name: 'td:nth-child(1)',
        value: 'td:nth-child(2)'
      }
    }
  }
};

// The `data` is an HTML Content of type string.
// https://www.amazon.com/AMD-Ryzen-3700X-16-Thread-Processor/dp/B07SXMZLPK/
const data = '<html>...</html>';

const result = parse(data, config);

Output

{
  "title": "AMD Ryzen 7 3700X 8-Core, 16-Thread Unlocked Desktop Processor with Wraith Prism LED Cooler",
  "price": "$308.99",
  "rating": 4.9,
  "features": [
    {
      "name": "Brand",
      "value": "AMD"
    },
    {
      "name": "CPU Model",
      "value": "AMD Ryzen 7"
    },
    {
      "name": "CPU Speed",
      "value": "4.4 GHz"
    },
    {
      "name": "CPU Socket",
      "value": "Socket AM4"
    },
    {
      "name": "Processor Count",
      "value": "8"
    }
  ]
}

License

Distributed under the MIT License. See LICENSE for more information.

muninn's People

Contributors

aykutkardas avatar fatihky avatar irensaltali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

muninn's Issues

How to handle arrays of objects

How to handle arrays of objects

This is my html

<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head> </head>
  <body>
    <div>
      <table>
        <tr>
          <td>
            <p><a href="#" onclick="return do_hathdl('780')">780x</a></p>
          </td>
          <td>
            <p><a href="#" onclick="return do_hathdl('980')">980x</a></p>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>

This is my config

{
  schema: {
    items: {
      // What should I do ?
      type: "array",
      regex: new RegExp(
        '<a href="[^"]*" onclick="return do_hathdl\\(\'([0-9]+|org)\'\\)">([^<]+)</a>',
      ),
    },
  },
}

Expect

{
  "items": [
    {
      "res": "780",
      "name": "780x"
    },
    {
      "res": "980",
      "name": "980x"
    }
  ]
}

Boolean fields

We may add support for boolean fields based on the existence of the selector/attribute.

Make library more generic

I think some changes would make this library suitable for more use cases. The changes are:

  1. Exporting array instead of object in configuration JSON to accept one than more selectors.
  2. Renaming blockSelector to selector.
  3. Renaming collection property to union.
  4. Accepting schema property alongside the selector to allow parse single type instead of union types.

`cli` package must be made for muninn

Some commands:

-d --dir Input directory Path
-f --file Input File Path
-c --config Config File Path
-o --output Output Directory Path

muninn --file index.html --config config.json --output output/

muninn --dir html/ --config config.json --output output/

Add Regex support

Regular expressions from config should be able to be applied on the data collected.

regex not working

Regex not working

This is my html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01">
<html>
	<head>
	    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<title>Please stand by...</title>
	</head>
	<body>
		<div id="redirectwrap">
			<h4>Thanks</h4>
			<p>You are now logged in as: lau8<br /><br />Please wait while we transfer you...</p>
		</div>
	</body>
</html>

This is my config

{
  schema: {
    username: {
      regex: new RegExp("<p>You are now logged in as: (.+?)<"),
    }
  },
}

Unfortunately, it doesn't work

Expect

working

discussion: number fields

We may add support for converting cheerio's result to number as an HTML may contain only numeric characters.

extracting arrays

queried elements may contain children with the same selector. we should allow people to define some fields as arrays.

example:
image

Adding "trim" option by default

Unnecessary spaces in the collected data should be removed. It should be able to be disabled with this configuration.

key: {
 selector: ".selector",
 trim: false
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.