2014-09-28 57 views
1

我想在perl中打印重定向的url。Perl打印重定向的url

輸入網址:http://pricecheckindia.com/go/store/snapdeal/52517?ref=velusliv

輸出網址:http://www.snapdeal.com/product/vox-2-in-1-camcorder/1154987704?utm_source=aff_prog&utm_campaign=afts&offer_id=17&aff_id=1298&source=pricecheckindia

use LWP::UserAgent qw(); 
use CGI qw(:all); 
print header(); 
my ($url) = "http://pricecheckindia.com/go/store/snapdeal/52517?ref=velusliv"; 
my $ua = LWP::UserAgent->new; 
my $req = new HTTP::Request(GET => $url); 
my $res = $ua->request($req); 
print $res->request; 

如何獲得在perl做了什麼?

+0

你能告訴我你想要得到什麼嗎?期望的輸出? – Praveen 2014-09-28 17:51:09

回答

2

您需要檢查HTTP response以查找URL。的HTTP::Response文檔給出瞭如何做到這一點,但概括全部細節,你應該做到以下幾點:

use strict; 
use warnings; 
use feature ':5.10'; # enables "say" 
use LWP::UserAgent; 
my $url = "http://pricecheckindia.com/go/store/snapdeal/52517?ref=velusliv"; 

my $ua = LWP::UserAgent->new; 
my $req = new HTTP::Request(GET => $url); 
my $res = $ua->request($req); 

# you should add a check to ensure the response was actually successful: 
if (! $res->is_success) { 
    say "GET failed! " . $res->status_line; 
} 

# show the base URI for the response: 
say "Base URI: " . $res->base; 
使用 HTTP::Responseredirects方法

您可以查看重定向:

if ($res->redirects) { # are there any redirects? 
    my @redirects = $res->redirects; 
    say join(", ", @redirects); 
} 
else { 
    say "No redirects."; 
} 

在這情況下,基本URI與$url相同,如果您檢查頁面的內容,則可以看到原因。

# print out the contents of the response: 
say $res->decoded_contents; 

右接近頁面的底部,有下面的代碼:

$(window).load(function() { 
     window.setTimeout(function() { 
      window.location = "http://www.snapdeal.com/product/vox-2-in-1-camcorder/1154987704?utm_source=aff_prog&utm_campaign=afts&offer_id=17&aff_id=1298&source=pricecheckindia" 
     }, 300); 
    }); 

重定向是由JavaScript處理,所以不會被LWP :: UserAgent的回升。如果你想得到這個URL,你需要從響應內容中提取它(或者使用支持javascript的不同客戶端)。

在不同的音符,你的腳本開始了這樣的:

use LWP::UserAgent qw(); 

模塊名稱,qw()下面的代碼,用於導入特定的子程序到腳本中,這樣就可以按名稱(使用它們而不必參考模塊名稱和子程序名稱)。如果qw()是空的,它什麼也沒做,所以你可以忽略它。

0

它會拋出最後一行$ res - >請求的錯誤,因爲它從響應返回散列和內容。所以,下面是代碼:

use LWP::UserAgent qw(); 
use CGI qw(:all); 
print header(); 
my ($url) = "http://pricecheckindia.com/go/store/snapdeal/52517?ref=velusliv"; 
my $ua = LWP::UserAgent->new; 
my $req = new HTTP::Request(GET => $url); 
my $res = $ua->request($req); 
print $res->content; 
1

要有LWP::UserAgent進行重定向,只需設置max_redirects選項:

use strict; 
use warnings; 

use LWP::UserAgent qw(); 

my $url = "http://pricecheckindia.com/go/store/snapdeal/52517?ref=velusliv"; 

my $ua = LWP::UserAgent->new(max_redirect => 5); 

my $res = $ua->get($url); 

if ($res->is_success) { 
    print $res->decoded_content; # or whatever 
} else { 
    die $res->status_line; 
} 

然而,該網站是使用JavaScript重定向。

$(window).load(function() { 
     window.setTimeout(function() { 
      window.location = "http://www.snapdeal.com/product/vox-2-in-1-camcorder/1154987704?utm_source=aff_prog&utm_campaign=afts&offer_id=17&aff_id=1298&source=pricecheckindia" 
     }, 300); 
    }); 

這是行不通的,除非你使用一個框架,使JavaScript的,像WWW::Mechanize::Firefox