LWP handles javascript redirection

Source: Internet
Author: User

Yunshu

In the evening, I carefully debugged the program and found a bug. I handled 302 redirect when capturing the page content, but did not handle javascript redirection. After a simple task, you can use it to optimize and make careful judgments.

Sub GetContentFromUrl
{
My $ url = shift;
My $ time_out = shift;
 
Chomp ($ url );
 
My $ ua = LWP: UserAgent-> new ();
$ Ua-> cookie_jar ({});
$ Ua-> agent (Mozilla/4.0 (compatible; MSIE 6.0; Windws NT 5.1 ));
$ Ua-> timeout ($ time_out );
 
My $ res_obj = $ ua-> get ($ url) | warn "get $ url error, $! N ";
Print "$ url:". $ res_obj-> status_line. "n ";
If (! $ Res_obj-> is_success)
{
Return undef;
}
 
My $ html = $ res_obj-> as_string;
If ($ html = ~ M/window. location [. href]? S * = s *"(.*?) "/Is)
{
My $ tmp_url = URI: URL-> new ($1, $ url );
$ Tmp_url = $ tmp_url-> abs;
Print "found window. location, will follow $ tmp_urln ";
 
& GetContentFromUrl ($ tmp_url, $ time_out );
}
Else
{
Return $ html;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.