UNPKG

10.6 kBMarkdownView Raw
1# node-goldwasher
2[![npm version](http://img.shields.io/npm/v/goldwasher.svg)](https://www.npmjs.org/package/goldwasher)
3[![Build Status](http://img.shields.io/travis/alexlangberg/node-goldwasher.svg)](https://travis-ci.org/alexlangberg/node-goldwasher)
4[![Coverage Status](http://img.shields.io/coveralls/alexlangberg/node-goldwasher.svg)](https://coveralls.io/r/alexlangberg/node-goldwasher?branch=master)
5[![Code Climate](http://img.shields.io/codeclimate/github/alexlangberg/node-goldwasher.svg)](https://codeclimate.com/github/alexlangberg/node-goldwasher)
6
7[![Dependency Status](https://david-dm.org/alexlangberg/node-goldwasher.svg)](https://david-dm.org/alexlangberg/node-goldwasher)
8[![devDependency Status](https://david-dm.org/alexlangberg/node-goldwasher/dev-status.svg)](https://david-dm.org/alexlangberg/node-goldwasher#info=devDependencies)
9
10The purpose module is to extract text information from HTML, usually a website, which will often have to be sanitized and filtered to be useful. This module takes a pile of HTML and washes out the parts you need as small, golden nuggets of text and related metadata, the default options referred to as the *goldwasher format*:
11
12JSON format (see additional formats in the bottom):
13```javascript
14{
15 timestamp: 1402847736380,
16 text: "Oak is strong and also gives shade.",
17 keywords: [
18 {word: "oak", count: 1},
19 {word: "strong", count: 1},
20 {word: "gives", count: 1},
21 {word: "shade", count: 1}
22 ],
23 href: "http://www.oakisstrong.com/oak/strong",
24 tag: "h1",
25 position: 0,
26 total: 2,
27 uuid: "808b7490-f743-11e4-90b2-df723554e9be",
28 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
29 source: "http://www.oakisstrong.com"
30}
31```
32
33It works by passing it either pure HTML as a string (e.g. from [request](https://www.npmjs.org/package/request)) or a [cheerio](https://www.npmjs.org/package/cheerio) object, usually along with a [cheerio](https://www.npmjs.org/package/cheerio) (jQuery) selector (HTML tags) from which the text should be extracted, along with other options. It will then return an array of nuggets (objects) of information - one per recognized tag. For each nugget, it will try to:
34
351. Get the text of the tag and sanitize it, e.g. remove newlines.
362. Optionally discard the nugget, if it matches an array of stop texts.
373. Get the exact time of processing.
384. Extract a count of each word in the text as keywords.
395. Optionally discard keywords that match an array of stop words.
406. Optionally discard keywords that match an external array of keywords (see the folder stop_words).
417. Extract the nearest URL of the closest link.
428. Extract the tag type of the matched target.
439. Assign a unique identifier (UUID V1).
4410. Index the nugget position in the order it was found found.
4511. Add the total nugget count.
4612. Add the URL of the original source.
4713. Assign a unique identifier (UUID V1) that is similar for the entire batch of nuggets.
48
49The returned nuggets include the object properties:
50
51- ```timestamp``` - the exact time the tag was processed.
52- ```text``` - a sanitized version of the text of the tag.
53- ```keywords``` - a count of each word in the text, special characters removed.
54- ```href``` - the closest link, the first that matches one of:
55 1. Is the tag itself a link?
56 2. Does the tag have a child node that is a link?
57 3. Is there a link if we traverse up the DOM tree?
58 4. Is there an adjecent (sibling) link?
59- ```tag``` - the type of tag that was processed.
60- ```position``` - the position of the object, indicating the order in which tags were found. 0-based.
61- ```total``` - total number of nuggets in relation to the position. 1-based.
62- ```uuid``` - a unique identifier (UUID V1).
63- ```batch``` - a unique identifier (UUID V1) that is the same for the entire batch of nuggets.
64- ```source``` - a URL that was scraped, also the same for all nuggets.
65
66
67Alternatively, the output can be configured as XML, Atom or RSS format with the ```output``` option. The reason redundant information is included, such as the source, is that each returned nugget is supposed to be an atomic piece of information. As such, each nugget is to contain the information that "somewhere, at some point in time, something was written (with a link to some place)".
68
69## Installation
70```
71npm install goldwasher
72```
73
74## Options
75- ```selector``` - cheerio (jQuery) selector, a selection of target tags.
76- ```search``` - only pick results containing these terms. Not case or special character sensitive (sluggified search).
77- ```limit``` - limit number of results.
78- ```url``` - base url of links, for sites that use relative urls.
79- ```filterTexts``` - stop texts that should be excluded.
80- ```filterKeywords``` - stop words that should be excluded as keywords.
81- ```filterLocale``` - stop words from external JSON file (see the folder stop_words).
82- ```output``` - output format (```json```, ```xml```, ```atom``` or ```rss```).
83- ```contractAdjecent``` - contracts adjecent targets. Useful for sites that split text, e.g. ```<div><h1>Hello</h1><br><h1>World<h1></div>```.
84
85## Example
86```javascript
87var goldwasher = require('goldwasher');
88
89var html = '<a href="/oak/strong"><h1>Oak is strong and also gives shade.</h1></a>';
90 html += '<h2><a href="http://www.catsanddogs.com/hate">Cats and dogs each hate the other.</a></h2>';
91 html += '<h2>Some unwanted text.</h2>';
92
93var options = {
94 selector: 'h1, h2',
95 url: 'http://www.oakisstrong.com',
96 filterTexts: ['Some unwanted text.'],
97 filterLocale: 'en',
98 filterKeywords: ['also']
99}
100
101var result = goldwasher(html, options);
102
103/* result:
104[
105 {
106 timestamp: 1402847736380,
107 text: "Oak is strong and also gives shade.",
108 keywords: [
109 {word: "oak", count: 1},
110 {word: "strong", count: 1},
111 {word: "gives", count: 1},
112 {word: "shade", count: 1}
113 ],
114 href: "http://www.oakisstrong.com/oak/strong",
115 tag: "h1",
116 position: 0,
117 total: 2,
118 uuid: "808b7490-f743-11e4-90b2-df723554e9be",
119 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
120 source: "http://www.oakisstrong.com"
121 },
122 {
123 timestamp: 1402847736381,
124 text: "Cats and dogs each hate the other.",
125 keywords: [
126 {word: "cats", count: 1},
127 {word: "dogs", count: 1},
128 {word: "hate", count: 1}
129 ],
130 href: "http://www.catsanddogs.com/hate",
131 tag: "h2",
132 position: 1,
133 total: 2,
134 uuid: "a48fbb30-f743-11e4-96e6-7b423a412011",
135 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
136 source: "http://www.oakisstrong.com"
137 }
138]
139*/
140```
141
142## Output formats
143**JSON:**
144```javascript
145{
146 timestamp: 1402847736380,
147 text: "Oak is strong and also gives shade.",
148 keywords: [
149 {word: "oak", count: 1},
150 {word: "strong", count: 1},
151 {word: "gives", count: 1},
152 {word: "shade", count: 1}
153 ],
154 href: "http://www.oakisstrong.com/oak/strong",
155 tag: "h1",
156 position: 0,
157 total: 2,
158 uuid: "808b7490-f743-11e4-90b2-df723554e9be",
159 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
160 source: "http://www.oakisstrong.com"
161}
162```
163
164**XML:**
165```xml
166<?xml version="1.0" encoding="UTF-8"?>
167<goldwasher>
168 <nugget>
169 <href>/oak/strong</href>
170 <tag>h1</tag>
171 <text>Oak is strong and also gives shade.</text>
172 <position>0</position>
173 <timestamp>1431296135800</timestamp>
174 <uuid>14eefda0-f762-11e4-a0b3-d5647c4f7651</uuid>
175 <total>3</total>
176 <batch>14eefda0-f762-11e4-a0b3-d5647c4f7651</batch>
177 <source>http://www.oakisstrong.com</batch>
178 <keywords>
179 <keyword>
180 <word>oak</word>
181 <count>1</count>
182 </keyword>
183 <keyword>
184 <word>is</word>
185 <count>1</count>
186 </keyword>
187 <keyword>
188 <word>strong</word>
189 <count>1</count>
190 </keyword>
191 <keyword>
192 <word>and</word>
193 <count>1</count>
194 </keyword>
195 <keyword>
196 <word>also</word>
197 <count>1</count>
198 </keyword>
199 <keyword>
200 <word>gives</word>
201 <count>1</count>
202 </keyword>
203 <keyword>
204 <word>shade</word>
205 <count>1</count>
206 </keyword>
207 </keywords>
208 </nugget>
209</goldwasher>
210```
211
212**Atom:**
213```xml
214<?xml version="1.0" encoding="utf-8"?>
215<feed xmlns="http://www.w3.org/2005/Atom">
216 <title>foo title</title>
217 <link>foo.com</link>
218 <updated>2015-05-12T16:12:45Z</updated>
219 <link rel="alternate" href="foo.com"/>
220 <subtitle>Foo Bar Baz</subtitle>
221 <generator>Feed for Node.js</generator>
222 <category term="oak"></category>
223 <category term="is"></category>
224 <category term="strong"></category>
225 <category term="and"></category>
226 <category term="also"></category>
227 <category term="gives"></category>
228 <category term="shade"></category>
229 <entry>
230 <title type="html"><![CDATA[Oak is strong and also gives shade.]]></title>
231 <id>foo.com/oak/strong</id>
232 <link href="foo.com/oak/strong">
233 </link>
234 <updated>2015-05-12T16:12:45Z</updated>
235 <summary type="html"><![CDATA[Oak is strong and also gives shade.]]></summary>
236 <author>
237 <name>Baz Barfoo</name>
238 <uri>foo.com</uri>
239 </author>
240 </entry>
241</feed>
242```
243
244**RSS:**
245```xml
246<?xml version="1.0" encoding="utf-8"?>
247<rss version="2.0">
248 <channel>
249 <title>foo title</title>
250 <description>Foo Bar Baz</description>
251 <link>foo.com</link>
252 <lastBuildDate>Tue, 12 May 2015 16:15:03 GMT</lastBuildDate>
253 <docs>http://blogs.law.harvard.edu/tech/rss</docs>
254 <generator>Feed for Node.js</generator>
255 <category>oak</category>
256 <category>is</category>
257 <category>strong</category>
258 <category>and</category>
259 <category>also</category>
260 <category>gives</category>
261 <category>shade</category>
262 <item>
263 <title><![CDATA[Oak is strong and also gives shade.]]></title>
264 <link>foo.com/oak/strong</link>
265 <guid>foo.com/oak/strong</guid>
266 <pubDate>Tue, 12 May 2015 16:15:03 GMT</pubDate>
267 <description><![CDATA[Oak is strong and also gives shade.]]></description>
268 <content:encoded/>
269 <author>
270 <name>Baz Barfoo</name>
271 <link>foo.com</link>
272 </author>
273 </item>
274 </channel>
275</rss>
276```