-
Notifications
You must be signed in to change notification settings - Fork 2
/
scraper.pl
163 lines (131 loc) · 3.77 KB
/
scraper.pl
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
#!/usr/bin/perl
use strict;
use warnings;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
package Mechanize::Resilient;
# The requests usually tak around 12s. The default LWP::UserAgent socket
# activity timeout is 180s. However, sometimes we're unlucky and get no
# response for 180s at all, while the subsequent request succeeds. Maybe
# this is a rotten webserver behind a retarded balancer? No ides, let's
# just try harder.
use LWP::ConnCache;
use WWW::Mechanize;
use base qw/WWW::Mechanize/;
sub get
{
my $resp;
foreach (0..3) {
$resp = WWW::Mechanize::get (@_);
last if $resp->is_success;
warn $resp->request->uri.': '.$resp->status_line;
}
return $resp;
}
sub new
{
my $self = WWW::Mechanize::new (@_);
$self->timeout (60);
$self->conn_cache(LWP::ConnCache->new(
'total_capacity' => 0 ));
return bless $self;
}
package main;
use URI;
use HTML::TreeBuilder;
use Database::DumpTruck;
use utf8;
use Unicode::Normalize;
my $root = new URI ('http://www.justice.gov.sk/Stranky/Ministerstvo/Vyberove-konania-v-rezorte/Zoznam-vyberovych-konani.aspx');
my $mech = new Mechanize::Resilient;
my $dt = new Database::DumpTruck ({ dbname => 'data.sqlite', table => 'swdata' });
sub do_detail
{
my $resp = shift;
my $tree = new_from_content HTML::TreeBuilder ($resp->decoded_content);
# This is what we deal with:
#
# <div class="DetailTable">
# <label class="popiska">Organizácia:</div>
# <div class="hodnota">Okresný súd Košice I</div>
# <div class="riadok"></div>
#
# <label class="popiska">Pozícia:</div>
# <div class="hodnota">absolvenská prax</div>
# <div class="riadok"></div>
# ...
# <div class="skupina">Prehľad prihlásených uchádzačov:</div>
# <div class="riadok ciara"></div>
#
# <div>
#
# </div>
# </div>
my ($table) = $tree->look_down (class => 'DetailTable');
my @divs = $table->look_down (_tag => qr/div|label/);
my %row;
my $popiska = '';
my $popiska_db = '';
my $hodnota = '';
foreach my $div (@divs) {
my $class = ($div->attr('class') || '');
if ($class eq 'popiska') {
$popiska = $div->as_trimmed_text;
# Beautify a bit!
$popiska =~ s/:$//;
# Remove diacritics and remove spaces and slashes
# so we can use data from page to create columns
$popiska_db = NFKD($popiska);
$popiska_db =~ s/\p{NonspacingMark}//g;
$popiska_db =~ s/[ \/]/_/g;
$popiska_db =~ s/[\.,]//g;
} elsif ($class eq 'hodnota') {
$hodnota = $div->as_trimmed_text;
# Is the value a link? Absolutize it!
my ($link) = @{$div->extract_links ('a')};
$hodnota = new URI ($link->[0])->abs ($resp->request->uri)->as_string
if $link;
if ($hodnota ne '') {
$row{$popiska_db} = $hodnota;
}
$popiska = '';
$popiska_db = '';
$hodnota = '';
}
}
print $row{"Datum_uzavierky"} . "\n";
$dt->upsert (\%row);
}
$dt->create_table(
{'Datum_uzavierky' => 'text',
'Miesto_vykonu_prace' => 'text',
'Organizacia' => 'text',
'Pozicia' => 'text',
'Stav' => 'text',
'Utvar_popis' => 'text',
'Vyhlasenie_konania' => 'text',
'Miesto_konania' => 'text',
'Obec' => 'text',
'PSC' => 'text',
'Tel_cislo' => 'text',
'Termin_konania' => 'text',
'Ulica_cislo_ulice' => 'text',
'Zlozenie_komisie' => 'text',
'Priebeh_konania' => 'text'},
'swdata');
$dt->create_index(['Vyhlasenie_konania'], undef, 'IF NOT EXISTS', 'UNIQUE');
# Roll!
$mech->get ($root);
do {
do_detail ($mech->clone->get ($_)) foreach
$mech->find_all_links (url_regex => qr/Detail-vyberoveho-konania.aspx/);
# The pager element. Retarded.
my ($pager) = $mech->find_all_inputs (name_regex => qw/cmbAGVPager$/);
$pager->value ($pager->value + 1);
# Page still valid?
if (grep { $_ == $pager->value } $pager->possible_values) {
$mech->submit_form;
} else {
# Destroy the agent, we're done.
$mech = undef;
}
} while ($mech);