-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
487 lines (418 loc) · 17.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Liberating Data OS//OS - @kayakr, Catalyst</title>
<meta name="author" content="Catalyst">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/catalyst.css" id="theme">
<!-- For syntax highlighting -->
<!-- <link rel="stylesheet" href="lib/css/zenburn.css"> -->
<link rel="stylesheet" href="lib/css/catalyst-syntax.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<style>
/* Add the ability to omit slides from the printed version (has to be after the print include)*/
@media print {
.reveal .slides section.no-print {
display: none !important;
visibility: hidden !important;
}
}
</style>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<!-- Please put any custom styles here -->
<style>
</style>
</head>
<body>
<div class="reveal">
<!--
0. Introduction.
1. Before scraping
1.1 Do you need to?
1.2 Understanding the page
2. Scraping with Kimono
2.1 Kimono
2.2 Using widget on MPs: nzmps
2.3 Step through API admin pages on Kimono
2.4 Modify results using js:
kimmodify=1
3. Scraping with node.js
3.1 node.js equivalent to kimono API.
4. Scraping with Tabula
5. Summary
5.1 Resources
https://github.com/kayakr/parliament.nz
http://phantomjs.org/
import.io
morph.io
-->
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Cover slide -->
<section>
<h1 class="catalyst-logo">Catalyst</h1>
<h3>Liberating Data</h3>
<p>How to get data from websites that lack APIs<br/>
using Kimono, node.js</p>
<p class="small-text">
Presented by Jonathan Hunt (<a href="https://twitter.com/kayakr">@kayakr</a>)
</p>
</section>
<!-- 0. Introduction -->
<!-- Section title slide with big colour dot -->
<section>
<section class="title-slide banner-5">
<h2 class="orange">Introduction</h2>
<h3>Scraping? APIs? Who am I?</h3>
</section>
<!-- Centered paragraph -->
<section>
<h3>What is the problem?</h3>
<p>Often websites present information, but don't make data available in a structured way<br/>e.g. as CSV, JSON, XML<br/>
-> "machine-readable"</p>
<p>e.g. List of MPs on parliament.nz</p>
<ul>
<li>Scraping parliament.nz with Kimono</li>
<li>Scraping parliament.nz using node.js</li>
<!--<li>List item</li>-->
</ul>
</section>
<section>
<h3>Jonathan Hunt</h3>
<ul>
<li>Web developer: PHP, Javascript, CSS</li>
<li>Open data advocate</li>
<li>FixMyStreet.org.nz</li>
<li>@kayakr on Twitter, Github</li>
</ul>
</section>
</section>
<!-- 1. before scraping -->
<section>
<section class="title-slide banner-8">
<h2 class="orange">Before scraping</h2>
</section>
<!-- what page are we looking at? -->
<section>
<img src="img/parliament.nz_mps.png"/>
</section>
<!-- Slide with list items -->
<section>
<h3>Do you need to do this?</h3>
<ul>
<li>Read page carefully - RSS? Atom?</li>
<li>Search: google et al</li>
<li>Is there any API already? (Kimono, etc.)</li>
<li>What is the copyright status?</li>
</ul>
</section>
<section>
<h3>Understand the page structure</h3>
<p>Inspect the page markup</p>
<p class="small-text">Chrome/Firefox: right-mouse, Inspect element</p>
<p>Are there elements, classes, ids that you can work with?</p>
<p>What data is visible (Last name, First name, ...)</p>
<p>Hidden? URLs, Ids</p>
</section>
</section>
<!-- 2. Scraping with kimono -->
<section>
<section class="title-slide banner-5">
<h2 class="orange">Scraping with Kimono</h2>
</section>
<section>
<h3>KimonoLabs.com</h3>
<p>Friendly tool to create APIs from web pages</p>
<ul>
<li>Free for public APIs (beer, not libre)</li>
<li>Chrome plugin, or bookmarklet</li>
</ul>
</section>
<section>
<h3>Kimono API: nzmps</h3>
<p><em>Use Kimono to generate a list of MPs</em></p>
<ol>
<li>Visit source page</li>
<li>Enable plugin</li>
<li>Interactively add properties</li>
<li>Extractor View, Data Model View, Raw Data View</li>
<li>Done: generates API (via kimono's servers)</li>
</ol>
<aside class="notes">
(assumes Kimono extension installed)
<ol>
<li>Visit page <a href="http://www.parliament.nz/en-nz/mpp/mps/current?Criteria.ViewAll=1">http://www.parliament.nz/en-nz/mpp/mps/current?Criteria.ViewAll=1</a></li>
<li>Click Kimono widget</li>
<li>Select MP name, (note how similar page elements are highlighted).<br/>
Click 'tick' to select similar elements<br/>
Enter 'name' in property label input<br/>
(note count, does it seem sane?)</li>
4. Click +, then Party, Electorate. Click 'tick', label as 'party_electorate'
5. Click Data Model View, for sample of data
Click Advanced to see selectors being used, e.g. table > tbody > tr > td > a
6. Click Raw Data View, to see what JSON, CSV etc. looks like
(note href is also extracted)
You could just download CSV and be done, if that's your aim.
7. Go back and add 3rd property (href), then use Advanced, and attributes to break out href into distinct key (instead of property of name).
8. Click 'Done' to create API
Name: nzmps
Tag: government
Crawl: Daily
http://www.kimonolabs.com/load?url=http%3A%2F%2Fwww.parliament.nz%2Fen-nz%2Fmpp%2Fmps%2Fcurrent%3FCriteria.ViewAll%3D1
</ol>
"API created"
https://www.kimonolabs.com/apis/7k8pg23e
</aside>
</section>
<section>
<h3>Administer your API</h3>
<p>Overview of Kimono administration interface</p>
<ul>
<li>‘Data preview’</li>
<li>‘API docs’: sample usage incl. curl, node.js, PHP<br/>
<span class="small-text" style="font-size:80%"><em>Note API key</em></span><br/>
<span class="small-text" style="font-size:80%">In Terminal, try curl...</span></li>
<aside class="notes">
<pre>curl --include --request GET "https://www.kimonolabs.com/api/7k8pg23e?apikey=fnPh2z4GBRS9u1WAFRr6UP7NM7Boaf68"</pre>
<p>More readable if piped into jq:</p>
<pre>curl --request GET "https://www.kimonolabs.com/api/7k8pg23e?apikey=fnPh2z4GBRS9u1WAFRr6UP7NM7Boaf68" | jq .</pre>
<pre>kimlimit=5</pre>
</aside>
<li>‘Modify results’: use javascript to munge data</li>
</ul>
</section>
<section>
<h3>Modify results, with javascript (1)</h3>
<p>Use example2 as model: .map() works on each row, parse party_electorate and add new distinct key-value pairs splitting on ,</p>
<pre style="width:100%; font-size: 0.5em"><code data-trim contenteditable>
function(data) {
function split_party_electorate(row) {
// Split on , and trim.
var parts = row['party_electorate'].split(',');
row['party'] = parts[0].trim();
row['electorate'] = parts[1].trim();
delete row.party_electorate;
return row;
}
for(var collection in data.results) {
data.results[collection] = data.results[collection].map(split_party_electorate);
}
return data;
}
</code></pre>
<p>Add parameter <code>kimmodify=1</code> to URL</p>
<aside class="notes">
<pre>curl "https://www.kimonolabs.com/api/7k8pg23e?apikey=fnPh2z4GBRS9u1WAFRr6UP7NM7Boaf68&kimmodify=1" | jq .</pre>
</aside>
</section>
<section>
<h3>Modify results, with javascript (2)</h3>
<p>Use similar process to breakout last & first names</p>
<pre style="width:100%; font-size: 0.5em"><code data-trim contenteditable>
// in function(data)
function parse_name(row) {
var parts = row.name.split(',');
row.lastname = parts[0].trim();
row.firstnames = parts[1].trim();
row.name = row.firstnames + " " + row.lastname;
return row;
}
for(var collection in data.results) {
data.results[collection] = data.results[collection].map(split_party_electorate);
data.results[collection] = data.results[collection].map(parse_name);
}
</code></pre>
<p>Add parameter <code>kimmodify=1</code> to URL</p>
<aside class="notes">
<pre>curl "https://www.kimonolabs.com/api/7k8pg23e?apikey=fnPh2z4GBRS9u1WAFRr6UP7NM7Boaf68&kimmodify=1" | jq .</pre>
</aside>
</section>
<section>
<h3>More with Kimono</h3>
<ul>
<li>Timed crawls</li>
<li>Using output of one API as input to next</li>
<li>nzmp API: parsing roles…</li>
</ul>
</section>
</section>
<!-- 3. Scraping with node.js -->
<section>
<section class="title-slide banner-5">
<h2 class="orange">Scraping with node.js</h2>
</section>
<section>
<h3>node.js equivalent to kimono API</h3>
<ul>
<li>Install <a href="https://nodejs.org/">node.js</a> (<a href="https://iojs.org/">io.js</a>)</li>
<li>Use console to test candidate selectors</li>
</ul>
</section>
<section>
<h3>node.js (0)</h3>
<ul>
<li>package.json<pre style="width:100%; font-size: 0.5em"><code data-trim contenteditable>
{
"name": "parliament.nz-scraper",
"version": "0.0.1",
"description": "Extract data about MPs from parliament.nz",
"main": "nzmps1.js",
"scripts": {
"start": "node nmps1.js"
},
"dependencies": {
"cheerio": "^0.18.0",
"request": "^2.51.0"
},
"engines": {
"node": "0.10.x"
},
"keywords": [
"node",
"express"
],
"license": "GPL"
}
</code></pre></li>
<li>npm install</li>
</ul>
</section>
<section>
<h3>node nzmps1.js (1)</h3>
<ul>
<li>Use request to do HTTP Request</li>
<li>cheerio to parse page data using CSS selectors</li>
</ul>
<pre style="width:100%; font-size: 0.5em"><code data-trim contenteditable>
var cheerio = require('cheerio');
var request = require('request');
var base = "http://www.parliament.nz";
var url = base + "/en-nz/mpp/mps/current?Criteria.ViewAll=1";
var mps = [];
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
//
$('.listing tr td a').each(function(i, element) {
var name = $(this).text().trim();
var href = $(this).attr('href');
mps[i] = {'name': name, 'href': base + href};
});
$('.listing>tbody tr td:last-of-type').each(function(i, element) {
mps[i].party_electorate = $(this).text().trim();
});
console.log(JSON.stringify(mps));
}
});
</code></pre>
</section>
</section>
<!-- 4. Scraping with Tabula -->
<section>
<section class="title-slide banner-5">
<h2 class="orange">Scraping with Tabula</h2>
</section>
<section>
<h3>Tabula</h3>
<ul>
<li><a href="http://tabula.technology/">http://tabula.technology/</a></li>
<li>Java web app run locally (<a href="http://127.0.0.1:8080">http://127.0.0.1:8080</a>)</li>
<li>Identify rectangular zone on PDF page</li>
<li>Output text from PDF</li>
</ul>
</section>
<section>
<h3>Install Tabula</h3>
<p>CLI version (<a href="https://github.com/tabulapdf/tabula-extractor/wiki/Using-the-command-line-tabula-extractor-tool">install</a>)</p>
<p><a href="https://github.com/tabulapdf/tabula-extractor">https://github.com/tabulapdf/tabula-extractor</a></p>
<ul>
<li><code>rvm install jruby-1.7.15</code></li>
<li><code>rvm use jruby-1.7.15</code></li>
<li><code>jruby -S gem install tabula-extractor</code></li>
<li><code>tabula --help</code></li>
</ul>
</section>
<section>
<h3>Tabula (example)</h3>
<ul>
<li>http://127.0.0.1:8080/</li>
<li>Export as CSV</li>
<li>Parse CSV with node.js</li>
</ul>
</section>
</section>
<!-- X. Summary -->
<section>
<section>
<h3>Resources</h3>
<ul>
<li><a href="https://github.com/kayakr/parliament.nz">github.com/kayakr/parliament.nz</a></li>
<li><a href="http://phantomjs.org/">phantomjs.org/</a></li>
<li><a href="http://import.io">import.io</a></li>
<li><a href="http://morph.io">morph.io</a></li>
</ul>
</section>
<section>
<h3>Go out and scrape!</h3>
</section>
</section>
</div>
<div class="footer">
<div class="scarf">
<div class="scarf-orange"></div>
<div class="scarf-yellow"></div>
<div class="scarf-blue"></div>
<div class="scarf-green"></div>
</div>
<div class="footer-inner">
<div>
<!-- NOTE: use only one of these. open-source-technologists is the standard -->
<p class="open-source-technologists">open source technologists</p>
<!-- <p class="freedom-to-innovate">freedom to innovate</p> -->
</div>
</div>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'none', // default/cube/page/concave/zoom/linear/fade/none
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>