Perl crawler

The following is a crawler program using Perl for crawling images. The Chinese explanation of each line of code is as follows:

#!/usr/bin/perl 
​use
strict; 
use warnings; 
use Mojo::UserAgent; 
use JSON; 
​#
Create a Mojo::UserAgent instance 
my $ua = Mojo::UserAgent->new; 
​#
Use get agent 
my $proxy = get_proxy(); 
​#
Set the proxy 
$ua->proxy($proxy); 
​#
Use the GET method to request 
my $res = $ua->get(''); 
​#
Check whether the request is successful 
if ( $res->is_success) { 
    # Get the response body 
    my $body = $res->body; 
​#
    Use the JSON module to parse the response body 
    my $json = JSON->new; 
    my $data = $json->decode($body ); 
​#
    Get the image URL 
    my $image_url = $data->{image_url}; 
​#
    Use Mojo::UserAgent to download the image 
    my $image_res = $ua->get($image_url); 
​#
    Check whether the image download is successful 
    if ($image_res->is_success) { 
        # Save the image to local 
        my $filename = 'snapchat_image.jpg'; 
        $image_res->body->save_to_file($filename); 
        print "Successfully downloaded and saved the image to $filename\n"; 
    } else { 
        print "Image download failed:", $image_res->status_message, "\n"; } } 
    else 
{ 
    print "Request failed:", $res->status_message, "\n"; 
} 
​#
From https: //www.duoip.cn/get_proxy Get proxy IP 
sub get_proxy { 
    my $ua = Mojo::UserAgent->new; 
    my $proxy = $ua->get('')->result->json->{proxy }; 
    return $proxy; 
}

This program first uses Mojo::UserAgent_proxy to obtain an IP. Then use this proxy to send a GET request to P and check whether the request is successful. If successful, get the image URL from the response body and use Mojo::UserAgent to download the image. Finally, save the downloaded image locally.

Guess you like

Origin blog.csdn.net/weixin_73725158/article/details/134047736
Recommended