Thread Probleme mit HTML::Parser und LWP::UserAgent (8 answers)
Opened by fealXX at 2012-03-26 13:18

Linuxer
 2012-03-26 18:56
#157055 #157055
User since
2006-01-27
3875 Artikel
HausmeisterIn

user image
Wenn ich in Deinem Code genau die Änderung vornehme, die pq bereits angemerkt hat, funktioniert es bei mir:

angepasster Code (+Einrückung bearbeitet)
Code (perl): (dl )
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
#! /usr/bin/perl
use strict;
use warnings;
use Encode;
use HTML::Parser;
use LWP::UserAgent;


my @links;

my $url = "http://google.de/";

my $ua = LWP::UserAgent->new();
my $response = $ua->get($url);
my $inhalt    = $response->decoded_content;

my $p = HTML::Parser->new();
$p->handler(start => \&start_handler,"self,tagname,attr");
$p->parse($inhalt);

foreach my $link (@links){
  my $linkziel = $link->[0];

  if (defined($linkziel)){
    print $linkziel."\n";
    if($linkziel =~ m/http/i ){
      print "Linktext: ",$link->[1],"\tURL: ",$link->[0],"\n";
    }
  }
}


sub start_handler{
  my ($self, $tag, $attr) = @_;
  return if($tag ne 'a');

  my ($link) = $attr->{href};
  my $text;

  $self->handler(text => sub{$text = shift;},"dtext");
  $self->handler(
      end => sub {
          my ($tag) = @_;
          if ($tag eq 'a'){
              push(@links,[$link,$text])
          }
      },
      "tagname"
  );

}



Ergebnis:

Code: (dl )
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
http://www.google.de/imghp?hl=de&tab=wi
Linktext: Bilder URL: http://www.google.de/imghp?hl=de&tab=wi
http://video.google.de/?hl=de&tab=wv
Linktext: Videos URL: http://video.google.de/?hl=de&tab=wv
http://maps.google.de/maps?hl=de&tab=wl
Linktext: Maps URL: http://maps.google.de/maps?hl=de&tab=wl
http://news.google.de/nwshp?hl=de&tab=wn
Linktext: News URL: http://news.google.de/nwshp?hl=de&tab=wn
http://www.google.de/shopping?hl=de&tab=wf
Linktext: Shopping URL: http://www.google.de/shopping?hl=de&tab=wf
https://mail.google.com/mail/?tab=wm
Linktext: Mail URL: https://mail.google.com/mail/?tab=wm
http://www.google.de/intl/de/options/
Linktext: � URL: http://www.google.de/intl/de/options/
/url?sa=p&pref=ig&pval=3&q=http://www.google.de/ig%3Fhl%3Dde%26source%3Diglk&usg=AFQjCNFjfPavRPBJrOKJS3MB2uzhpfN6zw
Linktext: iGoogle URL: /url?sa=p&pref=ig&pval=3&q=http://www.google.de/ig%3Fhl%3Dde%26source%3Diglk&usg=AFQjCNFjfPavRPBJrOKJS3MB2uzhpfN6zw
http://www.google.de/history/optout?hl=de
Linktext: Webprotokoll URL: http://www.google.de/history/optout?hl=de
/preferences?hl=de
https://accounts.google.com/ServiceLogin?hl=de&continue=http://www.google.de/
Linktext: Anmelden URL: https://accounts.google.com/ServiceLogin?hl=de&continue=http://www.google.de/
/advanced_search?hl=de
/language_tools?hl=de
/intl/de/ads/
/services/
/intl/de/about.html
http://www.google.com/ncr
Linktext: Google.com in English URL: http://www.google.com/ncr
/intl/de/policies/
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Probleme mit HTML::Parser und LWP::UserAgent