Hallo, ich benötige mal wieder Eure Hilfe.Ich will mir ein Perl Skript schreiben, was mir von der Seite http://xml.weather.yahoo.com/forecastrss?p=<CITYCODE>&u=c die XML Daten in Text Daten umwandelt.
Mein Problem ist, dass wenn ich die Seite mit LWP::Simple und der Methode get() hole, funktioniert mein Parsen der Seite nicht. Erst wenn ich die Seite mit LWP::Simple und der Methode get() hole un in einer Datei speicher, und mit dem Unix Befehl cat die Datei wieder auslese und nach Perl weiter leite, funktioniert das Parsen und umwandeln der Seite von XML -> Text perfekt.
Weiss jemand wie ich es (ohne Shell Skript) in einem Perl Skript das lösen kann? Ich denke das liegt daran, dass der Unix Befehl die Datei Zeilenweise einliest und der Perl Schalter -n diese Zeilen in eine Schleife durchläuft und daher mein Parsen funktioniert.
Hier mein Skript:
#!/bin/bashID="GMXX0007" # Berlin Code
# Get the raw xml data from weather page.
perl -MLWP::Simple -e '
my $page = get( "http://xml.weather.yahoo.com/forecastrss?p='$ID'&u=c" );
open(DAT, "> erg.xml") or die( "Cant open erg.xml" );
print DAT $page;
close(DAT);
}'
# Convert raw xml data to text.
cat erg.xml | perl -ne '
$data{$1}{$2} = $4 if ( $_ =~ m/<(yweather|geo):([a-z]+)(?:(\s|>)+)([^>]+)/g );
$img{$1} = $2 if ( $_ =~ m/<(img)\s+src="([^"]+)"/g );
#$data{$1}{$2} = $4 if ( $page =~ m/<(yweather|geo):([a-z]+)(?:(\s|>)+)([^>]+)/g );
#$img{$1} = $2 if ( $page =~ m/<(img)\s+src="([^"]+)"/g );
END {
foreach my $k1( sort keys %data ) {
for my $k2( keys %{$data{$k1}} ) {
if ( $k1 eq "geo" ) { my $geo = $1 if ( $data{$k1}{$k2} =~ m/([^<]+)/g); $data{$k1}{$k2} = $geo; }
my @t = split( /("\s+|\s+")/, $data{$k1}{$k2} );
foreach my $k3( @t ) {
if ( $k3 =~ m/=/g ) {
my ( $keys, $value ) = split( /=/, $k3 );
$value =~ s/["\/]//g;
$data{$k1}{$k2}{$keys} = $value;
}
}
}
}
# Get the image path, cut the image name.
my ( $imgpath ) = ( $img{img} =~ m/^(.*?)\d+\.gif/g );
# Convert speed from kph to km/h
my $speed = $data{"yweather"}{"units"}{"speed"};
$speed = "km/h" if ( lc( $data{"yweather"}{"units"}{"speed"} ) eq "kph" );
# Convert the temperatur unit from C to °C.
my $degree = $data{"yweather"}{"units"}{"temperature"};
$degree = "°C" if ( lc( $data{"yweather"}{"units"}{"temperature"} ) eq "c" );
# Output.
print "longitude = ".$data{"geo"}{"long"}."\n";
print "latitude = ".$data{"geo"}{"lat"}."\n";
print "city = ".$data{"yweather"}{"location"}{"city"}."\n";
print "region = ".$data{"yweather"}{"location"}{"region"}."\n";
print "country = ".$data{"yweather"}{"location"}{"country"}."\n";
print "humidity = ".$data{"yweather"}{"atmosphere"}{"humidity"}."\n";
print "visibility = ".$data{"yweather"}{"atmosphere"}{"visibility"}." ".$data{"yweather"}{"units"}{"distance"}."\n";
print "pressure = ".$data{"yweather"}{"atmosphere"}{"pressure"}."\n";
print "rising = ".$data{"yweather"}{"atmosphere"}{"rising"}."\n";
print "sunrise = ".$data{"yweather"}{"astronomy"}{"sunrise"}."\n";
print "sunset = ".$data{"yweather"}{"astronomy"}{"sunset"}."\n";
print "current_code = ".$data{"yweather"}{"condition"}{"code"}."\n";
print "current_date = ".$data{"yweather"}{"condition"}{"date"}."\n";
print "current_temp = ".$data{"yweather"}{"condition"}{"temp"}." ".$degree."\n";
print "current_text = ".$data{"yweather"}{"condition"}{"text"}."\n";
print "current_image = ".$imgpath.$data{"yweather"}{"condition"}{"code"}.".gif\n";
print "speed = ".$data{"yweather"}{"wind"}{"speed"}." ".$speed."\n";
print "direction = ".$data{"yweather"}{"wind"}{"direction"}."\n";
print "chill = ".$data{"yweather"}{"wind"}{"chill"}."\n";
print "tomorrow_day = ".$data{"yweather"}{"forecast"}{"day"}."\n";
print "tomorrow_date = ".$data{"yweather"}{"forecast"}{"date"}."\n";
print "tomorrow_temp_low = ".$data{"yweather"}{"forecast"}{"low"}." ".$degree."\n";
print "tomorrow_temp_high = ".$data{"yweather"}{"forecast"}{"high"}." ".$degree."\n";
print "tomorrow_text = ".$data{"yweather"}{"forecast"}{"text"}."\n";
print "tomorrow_code = ".$data{"yweather"}{"forecast"}{"code"}."\n";
print "tomorrow_image = ".$imgpath.$data{"yweather"}{"forecast"}{"code"}.".gif\n";
}
'
# EOF
Datum: 17.11.2008-22:01
