# html-extractor

> Extract meta-data from a html string. It extracts the body, title, meta-tags and first headlines to a object to push them to a search indexer like elastic-search

Latest version **0.2.2** (published 2016-07-01) · 0 weekly downloads

## Install

```sh
npm install html-extractor
pnpm add html-extractor
yarn add html-extractor
bun add html-extractor
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2016-07-01 |
| First published | 2013-04-09 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.8.10 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mathias Peter |
| Maintainers | tcs-de |
| Keywords | html, parse, extract, body, search, tool, elastic, headlines, meta, data |

## Links

- npm: https://www.npmjs.com/package/html-extractor
- Repository: https://github.com/mpneuried/html-extractor
- Issues: https://github.com/mpneuried/html-extractor/issues
- npm.io page: https://npm.io/package/html-extractor

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) 4.x
- [htmlparser2](https://npm.io/package/htmlparser2.md) 3.9.x

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.2.2 (latest) — 2016-07-01
- 0.2.1 — 2016-06-30
- 0.2.0 — 2016-06-20
- 0.1.3 — 2013-06-20
- 0.1.2 — 2013-04-10
- 0.1.1 — 2013-04-09
- 0.1.0 — 2013-04-09

## README

html-extractor
==============

[![Build Status](https://secure.travis-ci.org/mpneuried/html-extractor.png?branch=master)](http://travis-ci.org/mpneuried/html-extractor)
[![Windows Tests](https://img.shields.io/appveyor/ci/mpneuried/html-extractor.svg?label=Windows%20Test)](https://ci.appveyor.com/project/mpneuried/html-extractor)
[![Dependency Status](https://david-dm.org/mpneuried/html-extractor.png)](https://david-dm.org/mpneuried/html-extractor)
[![NPM version](https://badge.fury.io/js/html-extractor.png)](http://badge.fury.io/js/html-extractor)

Extract meta-data from a html string. It extracts the body, title, meta-tags and first headlines to a object to push them to a search indexer like elastic-search

[![NPM](https://nodei.co/npm/html-extractor.png?downloads=true&stars=true)](https://nodei.co/npm/html-extractor/)

## Install

```
	npm install html-extractor
```

## Initialize


```js
var Extrator = require("html-extractor");
var myExtrator = new Extrator();
```

### `new Extrator( debug )`

**arguments**
- **debug** : *( `Boolean` optional: default = `false` )*  
Output the parsing time

## Methods

### Extrator.extract( html[, reduced], cb )

Call `.extract()` to get the data of an html string.  
HTML entities will be decoded.

**arguments:**

- **html** : *( `String` required )*  
The html string to process
- **reduced** : *( `Object` optional )*  
A object to reduce the content of body to a specific site content. It is not possible to reduce to a tag without a attribute filter.
	- **reduced.tag** : *( `String` required if `reduced` is set )*  
	The tag name of the html element to reduce to
	- **reduced.attr** : *( `String` required if `reduced` is set )*  
	The attribute of the html element to reduce to
	- **reduced.val** : *( `String` required if `reduced` is set )*  
	The attribute value of the html element to reduce to
	- **reduced.list** : *( `Boobean` default = `false` )*  
	Return every found reduced block as an array within body.
- **cb** : *( `Function` required )*  
The callback function

**callback arguments:**

- **error** : *( `Error` )*  
Error information. If no error occoured this will be `null`
- **data** : *( `Object` )*  
The extraction result
	- **data.body** : *( `String|Array` )*  
	The whole body content or the content within the configured reduced element. There will be just the text content without html tags/attributes and without the content in script tags.
	If the reduced feature is used and `reduced.list = true` the body will be an array of all found reduced blocks.  
	- **data.h1** : *( `Array` )*  
	An array containing all `h1` text contents. Including the `h1`elements outside the configured reduced element 
	- **data.meta** : *( `Object` )*  
	A Object of all found meta tags with the syntax `<meta content="" name="">`. Other meta tags will be ignored.
		- **data.meta.charset** : *( `String` optional )*  
		If a metatag with the charset setting like `<meta charset="utf-8" >` is defined it will be returned under `data.meta.charset`
		- **data.meta.title** : *( `String` default = `""` )*  
		If tilte tag is defined it will be returned under `data.meta.title`. Otherwise the key will contain an empty string
		- **data.meta.description** : *( `String` default = `""` )*  
		If a metatag with the name `description` is defined it will be returned under `data.meta.description`. Otherwise the key will contain an empty string
		- **data.meta.keywords** : *( `Array` default = `[]` )*  
		If a metatag with the name `keywords` is defined it will be returned as trimmed array of strings under `data.meta.keywords`. Otherwise the key will contain an empty string

## Examples

### simple

This is a simple example to extarct the content of a html document

```js
var Extrator = require("html-extractor");
var myExtrator = new Extrator();

var html = `
<html> 
	<head>
		<title>Testpage</title>
	</head>
	<body>
		<h1>Header 1</h1>
		<p>Content</p>
	</body>
</html>
`

myExtrator.extract( html, function( err, data ){
	if( err ){
		throw( err )
    } else {
		console.log( data );
		// {
		// 	meta: {
		// 		title: 'Testpage',
		//		description: '',
		//		keywords: []
		//	},
		//	body: ' Header 1 Content ',
		//	h1: [ 'Header 1' ]
		// }
    }
});
```

> see `test/readme_example_simple` or [run in Tonic](https://tonicdev.com/mpneuried/5767a1b1444f3a1400e793c2)

### advanced

This is a advanced example to show the usage of the reducing.
With the reduce feature it is possible to reduce the body content to the content of a specific html element.

```js
var Extrator = require("html-extractor");
var myExtrator = new Extrator();

var html = `
<html>
	<head>
		<title>Super page</title>
		<meta content="X, Y, Z" name="keywords">
		<meta content="Look at this super page" name="description">
		<meta content="Super pageCMS" name="generator">
	</head>
	<body>
		<div id="head">
			<h1>My super page<sup>2</sup></h1>
		</div> 
		<ul id="menu">
			<li>Home</li>
			<li>First</li>
			<li>Second</li>
		</ul>
		<div id="content">
			<h1>First article &euro;</h1>
			<p>Lorem ipsum dolor sit amet ... </p>
			<h1>Second article&nbsp;&nbsp;...&nbsp;&nbsp;</h1>
			<p>Aenean commodo ligula eget dolor.</p>
			<script>
				var superVar = [ 3,2,1 ]
			</script>
		</div>
		<section class="abc">
			<h3>ABC 1</h3>
			<p>Lorem ipsum dolor sit amet ... </p>
		</section>
		<section class="xyz">
			<h3>XYZ 1</h3>
			<p>Lorem ipsum dolor sit amet ... </p>
		</section>
		<section class="abc">
			<h3>ABC 2</h3>
			<p>Lorem ipsum dolor sit amet ... </p>
		</section>
		<div id="footer">
			Copyright 2013
		</div>
	</body>
</html>
`

var reduceTo = {
	tag: "div",
	attr: "id",
	val: "content"
}

myExtrator.extract( html, reduceTo, function( err, data ){
	if( err ){
		throw( err )
	} else {
		console.log( "String", data );
		//{
		//	meta: {
		//		title: 'Super page',
		//		description: 'Look at this super page',
		//		keywords: ['X', 'Y', 'Z'],
		//		generator: 'Super pageCMS'
		//	},
		//	body: 'First article € Lorem ipsum dolor sit amet ... Second article  ... Aenean commodo ligula eget dolor. ',
		//	h1: ['My super page2', 'First article €', 'Second article  ...']
		//}
	}
});

var reduceToList = {
	tag: "div",
	attr: "id",
	val: "content",
	list: true
}; 

myExtrator.extract( html, reduceToList, function( err, data ){
	if( err ){
		throw( err )
	} else {
		console.log( "List", data );
		//{
		//	meta: {
		//		title: 'Super page',
		//		description: 'Look at this super page',
		//		keywords: ['X', 'Y', 'Z'],
		//		generator: 'Super pageCMS'
		//	},
		//	body: [
		//		'ABC 1 Lorem ipsum dolor sit amet ... ',
		//		'ABC 2 Lorem ipsum dolor sit amet ... '
		//	],
		//	h1: ['My super page2', 'First article', 'Second article']
		//}
	}
});
```

> see `test/readme_example_advanced` or [run in Tonic](https://tonicdev.com/mpneuried/5767a178b29b431300aeb02f)

## Work in progress

`html-extractor` is work in progress. Your ideas, suggestions etc. are very welcome.

## Release History
|Version|Date|Description|
|:--:|:--:|:--|
|0.2.2|2016-07-1|Fixed trimming when `reduced.list` is active #3. Thanks to [Javier Castro](https://github.com/jacargentina)|
|0.2.1|2016-06-30|Fixed handling of html entities #1. Thanks to [Javier Castro](https://github.com/jacargentina)|
|0.2.0|2016-06-20|Added option to return reduced elements as list; Fixed reduced value check for classes; Optimized dev env.|
|0.1.4|-|Updated and pinned dependencies and optimized tests|
|0.1.3|-|Fixed extraction to remove style-tag content|
|0.1.2|-|Updated documentation|
|0.1.1|-|Added raw documentation; Fixed `travis.yml` |
|0.1.0|-|Initial version|

[![NPM](https://nodei.co/npm-dl/html-extractor.png?months=6)](https://nodei.co/npm/html-extractor/)

## License 

(The MIT License)

Copyright (c) 2016 M. Peter, http://www.tcs.de

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---
_Source: https://npm.io/package/html-extractor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
