Leser: 22
1 2 3 4 5 6 7
my $abstract; for my $h3 ( $html->look_down( _tag => 'h3')) { next if($h3->as_text ne 'Abstract'); $abstract = $h3->as_text; last; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<h3 class="blue">Abstract</h3>
<p class="blue">
The definition and purpose of crowdsourcing and its relevance to libraries is
discussed with particular reference to the
<a href="http://newspapers.nla.gov.au">Australian Newspapers</a>
service, <a href="http://familysearchindexing.org">FamilySearch</a>,
<a href="http://wikipedia.org">Wikipedia</a>,
<a href="http://www.pgdp.net">Distributed Proofreaders</a>,
<a href="http://www.galaxyzoo.org">Galaxy Zoo</a>
and <a href="http://mps-expenses.guardian.co.uk">The Guardian MP's Expenses Scandal</a>.
These services have harnessed thousands of digital volunteers who
transcribe, create, enhance and correct text, images and archives. Known
facts about crowdsourcing are presented and helpful tips and strategies for
libraries beginning to crowdsource are given. </p>
my $abstract = $document->findvalue('//h3[text() = "Abstract"]/following-sibling::p[1]//text()');
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
my $abstract; for my $h3 ($block->look_down(_tag => 'h3')) { # "ne" geht auch next if($h3->as_text() !~ /Abstract/i); # das nachfolgende Element: my $p=$h3->parent()->content()->[$h3->pindex()+1]; # etwas gefunden, # das eine Referenz(Objekt) ist # und den Tagnamen "p" hat? if($p && ref($p) && $p->tag() eq 'p') { $abstract= $p->as_text(); last; } } if($abstract) { ... }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ perl -wle'
use HTML::TreeBuilder::XPath;
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse(<<EOM);
<h3 class="blue-space">D-Lib Magazine</h3>
<p class="blue">March/April 2010<br />
Volume 16, Number 3/4<br />
<a href="../03contents.html">Table of Contents</a>
</p>
EOM
my $href = $tree->findvalue(q{//p[@class="blue"]/a/@href});
print $href'
../03contents.html
2010-04-14T14:38:32 pq[...]
edit: wobei ich mich frage, warum
$tree->findvalue(q{//a[text() = "Table of Contents"/@href});
nicht geht. da hörts dann doch mit meinen xpath-kenntnissen auf.
$tree->findvalue("//a[text() = \"Table of Contents\"]/attribute::href")
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
my $contents; outer:for my $h3 ($block->look_down(_tag => 'h3')) { # oder wie du den passenden "h3" Tag findest next if($h3->as_text() !~ /D-Lib Magazine/i); # Eine Ebene höner -> Ein Element weiter "p" my $p=$h3->parent()->content()->[$h3->pindex()+1]; if($p && ref($p) && $p->tag() eq 'p') { #################################### # suche nach Tag "a" my $ahref=$p->look_down(_tag=>'a'); if($ahref && $ahref->attr('href')) { $contents= $ahref->attr('href'); last; } #################################### # alternativ: for my $ahref ($p->content_list) { if($ahref && ref($ahref) && $ahref->tag() eq 'a' && $ahref->attr('href')) { $contents= $ahref->attr('href'); last outer; } } #################################### # alternativ2: (wenn man weiß wo das Element ist) my $ahref = $p->content()->[5] if($ahref && ref($ahref) && $ahref->attr('href')) { $contents= $ahref->attr('href'); last; } } } if($contents) { ... }
1 2 3 4 5
my $contents=$html->look_down(_tag=>'a',sub{$_[0]->as_text()=~/Table of Contents/i && $_[0]->attr('href')}); if($contents->attr('href')) { ... }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
if($contents) { my $content_data=get($contents); my $html2 = HTML::TreeBuilder->new(); $html2->parse($content_data); for my $p ($html2->look_down(_tag => 'p')) { next if($p->as_text() !~ /ISSN:/i); } if($p) { my $doi=$p->content()->[10]; $values{doi}=$doi if($doi); my $issn=$p->content()->[12]; $values{issn}=$issn if($issn); } $html2->delete(); }
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
if($contents) { my $content_data=get($contents); my $html2 = HTML::TreeBuilder->new(); $html2->parse($content_data); for my $p ($html2->look_down(_tag => 'p')) { next if($p->as_text() !~ /ISSN:/i); # # so geht das natürlich # my $doi=$p->content()->[10]; # $values{doi}=$doi if($doi); # my $issn=$p->content()->[12]; # $values{issn}=$issn if($issn); # wenn du dir nicht sicher bist wo genau die Daten stehen: for my $line ($p->content_list()) { # wir suchen keine "HTML::Element" Objekte (hier das <br />) # einfacher text gibt bei "ref" "false" zurück next if(ref($line)); # reguläre Ausdrücke zum finden der Zeilem mit "doi:" und "ISSN:" # gleichzeitiges holen der gesuchten Daten in "$1"; $values{doi}=$1 if($line=~/doi:(.+?)$/i); $values{issn}=$1 if($line=~/ISSN:(.+?)$/i); } # schleife abbrechen last; } $html2->delete(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use URI; ... # nutze "URI" um relative Pfade mit einer Vollständigen URL # in absolute um zu wandeln # EDIT: Das ist falsch im Zusammenhang mit dem restlichen Code # v #my $url_contents=URI->new_abs($contents,$values->{link}); # EDIT: # hier haben wir ja einen Hash und keine Hash-Referenz # Richtig: my $url_contents=URI->new_abs($contents,$values{link}); my $content_data=get($url_contents); ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Das ist das LWP::UserAgent-Objekt, # über das du nun alle Zugriffe machst # Das sollte recht früh initalisiert werden my $http_get=LWP::UserAgent->new(); ... # irgend ein link: my $site='http://www.dlib.org/rss/dlib.rss'; ... my $response=$http_get->get($site); unless($response->is_success()) { die(qq(ERROR: could not get "$site" MESSAGE:).$response->as_string()."\n"); } print "REAL URL:".$response->base()->as_string()."\n"; my $xml_data=$response->decoded_content(); ...
1 2 3 4 5 6 7 8
use URI; use XML::LibXML; my $document = XML::LibXML->new()->parse_html_file('http://www.example.com/') or die 'Could not load page'; my $document_uri = URI->new($document->URI); my $link_uri = URI->new_abs($document->findvalue('//a[1]/attribute::href'), $document_uri); say "$link_uri";