UNPKG

10.5 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
84## Example
85```javascript
86var goldwasher = require('goldwasher');
87
88var html = '<a href="/oak/strong"><h1>Oak is strong and also gives shade.</h1></a>';
89 html += '<h2><a href="http://www.catsanddogs.com/hate">Cats and dogs each hate the other.</a></h2>';
90 html += '<h2>Some unwanted text.</h2>';
91
92var options = {
93 selector: 'h1, h2',
94 url: 'http://www.oakisstrong.com',
95 filterTexts: ['Some unwanted text.'],
96 filterLocale: 'en',
97 filterKeywords: ['also']
98}
99
100var result = goldwasher(html, options);
101
102/* result:
103[
104 {
105 timestamp: 1402847736380,
106 text: "Oak is strong and also gives shade.",
107 keywords: [
108 {word: "oak", count: 1},
109 {word: "strong", count: 1},
110 {word: "gives", count: 1},
111 {word: "shade", count: 1}
112 ],
113 href: "http://www.oakisstrong.com/oak/strong",
114 tag: "h1",
115 position: 0,
116 total: 2,
117 uuid: "808b7490-f743-11e4-90b2-df723554e9be",
118 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
119 source: "http://www.oakisstrong.com"
120 },
121 {
122 timestamp: 1402847736381,
123 text: "Cats and dogs each hate the other.",
124 keywords: [
125 {word: "cats", count: 1},
126 {word: "dogs", count: 1},
127 {word: "hate", count: 1}
128 ],
129 href: "http://www.catsanddogs.com/hate",
130 tag: "h2",
131 position: 1,
132 total: 2,
133 uuid: "a48fbb30-f743-11e4-96e6-7b423a412011",
134 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
135 source: "http://www.oakisstrong.com"
136 }
137]
138*/
139```
140
141## Output formats
142**JSON:**
143```javascript
144{
145 timestamp: 1402847736380,
146 text: "Oak is strong and also gives shade.",
147 keywords: [
148 {word: "oak", count: 1},
149 {word: "strong", count: 1},
150 {word: "gives", count: 1},
151 {word: "shade", count: 1}
152 ],
153 href: "http://www.oakisstrong.com/oak/strong",
154 tag: "h1",
155 position: 0,
156 total: 2,
157 uuid: "808b7490-f743-11e4-90b2-df723554e9be",
158 batch: "14eefda0-f762-11e4-a0b3-d5647c4f7651",
159 source: "http://www.oakisstrong.com"
160}
161```
162
163**XML:**
164```xml
165<?xml version="1.0" encoding="UTF-8"?>
166<goldwasher>
167 <nugget>
168 <href>/oak/strong</href>
169 <tag>h1</tag>
170 <text>Oak is strong and also gives shade.</text>
171 <position>0</position>
172 <timestamp>1431296135800</timestamp>
173 <uuid>14eefda0-f762-11e4-a0b3-d5647c4f7651</uuid>
174 <total>3</total>
175 <batch>14eefda0-f762-11e4-a0b3-d5647c4f7651</batch>
176 <source>http://www.oakisstrong.com</batch>
177 <keywords>
178 <keyword>
179 <word>oak</word>
180 <count>1</count>
181 </keyword>
182 <keyword>
183 <word>is</word>
184 <count>1</count>
185 </keyword>
186 <keyword>
187 <word>strong</word>
188 <count>1</count>
189 </keyword>
190 <keyword>
191 <word>and</word>
192 <count>1</count>
193 </keyword>
194 <keyword>
195 <word>also</word>
196 <count>1</count>
197 </keyword>
198 <keyword>
199 <word>gives</word>
200 <count>1</count>
201 </keyword>
202 <keyword>
203 <word>shade</word>
204 <count>1</count>
205 </keyword>
206 </keywords>
207 </nugget>
208</goldwasher>
209```
210
211**Atom:**
212```xml
213<?xml version="1.0" encoding="utf-8"?>
214<feed xmlns="http://www.w3.org/2005/Atom">
215 <title>foo title</title>
216 <link>foo.com</link>
217 <updated>2015-05-12T16:12:45Z</updated>
218 <link rel="alternate" href="foo.com"/>
219 <subtitle>Foo Bar Baz</subtitle>
220 <generator>Feed for Node.js</generator>
221 <category term="oak"></category>
222 <category term="is"></category>
223 <category term="strong"></category>
224 <category term="and"></category>
225 <category term="also"></category>
226 <category term="gives"></category>
227 <category term="shade"></category>
228 <entry>
229 <title type="html"><![CDATA[Oak is strong and also gives shade.]]></title>
230 <id>foo.com/oak/strong</id>
231 <link href="foo.com/oak/strong">
232 </link>
233 <updated>2015-05-12T16:12:45Z</updated>
234 <summary type="html"><![CDATA[Oak is strong and also gives shade.]]></summary>
235 <author>
236 <name>Baz Barfoo</name>
237 <uri>foo.com</uri>
238 </author>
239 </entry>
240</feed>
241```
242
243**RSS:**
244```xml
245<?xml version="1.0" encoding="utf-8"?>
246<rss version="2.0">
247 <channel>
248 <title>foo title</title>
249 <description>Foo Bar Baz</description>
250 <link>foo.com</link>
251 <lastBuildDate>Tue, 12 May 2015 16:15:03 GMT</lastBuildDate>
252 <docs>http://blogs.law.harvard.edu/tech/rss</docs>
253 <generator>Feed for Node.js</generator>
254 <category>oak</category>
255 <category>is</category>
256 <category>strong</category>
257 <category>and</category>
258 <category>also</category>
259 <category>gives</category>
260 <category>shade</category>
261 <item>
262 <title><![CDATA[Oak is strong and also gives shade.]]></title>
263 <link>foo.com/oak/strong</link>
264 <guid>foo.com/oak/strong</guid>
265 <pubDate>Tue, 12 May 2015 16:15:03 GMT</pubDate>
266 <description><![CDATA[Oak is strong and also gives shade.]]></description>
267 <content:encoded/>
268 <author>
269 <name>Baz Barfoo</name>
270 <link>foo.com</link>
271 </author>
272 </item>
273 </channel>
274</rss>
275```