Skip to content

Instantly share code, notes, and snippets.

@kidd
Created February 5, 2010 06:27
Show Gist options
  • Select an option

  • Save kidd/295567 to your computer and use it in GitHub Desktop.

Select an option

Save kidd/295567 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TreeBuilder;
use LWP::UserAgent;
use Data::Dump qw(ddx dump);
use Memoize;
use feature ':5.10';
sub getShippingRate {
#http://www.iberlibro.com/servlet/ShipRates?vid=51947087
my ($id) = @_;
my $response = LWP::UserAgent->new->request(
HTTP::Request->new( GET => "http://www.iberlibro.com/servlet/ShipRates?vid=$id")); #$id
my $tree = HTML::TreeBuilder->new();
$tree->parse($response->content);
$tree->eof;
my $t = ($tree->look_down(
'class','data'
))[0];
my $row = ($t->look_down('_tag' , 'tr'))[1];
my $col = ($row->look_down('_tag', 'td'))[1];
$col->as_text =~ /EUR (.+)/;
return $1;
}
sub fixPaths {
my ($root) = @_;
foreach my $link (
$root->look_down('_tag','a',
sub {
return unless $_[0]->attr('href') =~ /^\//;
$_[0]->attr('href', "http://iberlibro.com". $_[0]->attr('href'));
}
))
{
# say $link->dump; #
}}
memoize('getShippingRate');
my $tree = HTML::TreeBuilder->new;
$tree->parse_file('sr.html');
#$tree->dump;
my $root = $tree->root;
#fixPaths($root);
# print $tree->as_HTML;
my @a = $root->look_down('_tag','table', 'class','result');
for (my $i=0 ; $i<@a ; $i++) {
$i++;
my $price = $a[$i]->look_down('class','price');
# print $a[$i]->dump;
#die;
my $scndInfo = $a[$i]->look_down('_tag' , 'div' , 'class','scndInfo');
# print $price->as_text , " <-preu \n";
my @link = $scndInfo->look_down('_tag', 'a');
# print $link[0]->dump;
# die;
my ($id) = $link[0]->attr('href') =~ /vid=(.+)$/;
#http://www.iberlibro.com/servlet/ShipRates?vid=51947087
# say getShippingRate($1);
my $element = HTML::Element->new('b');
$price->as_text =~ /EUR (.+)/;
# say $1;
# say $id;
$element->push_content(getShippingRate($id) + $1);
$scndInfo->push_content($element);
}
#ddx \@b;
#print $_->dump;
fixPaths($root);
print $tree->as_HTML;
$tree->delete;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment