tread
tread

Reputation: 11098

Perl: How to post specific data to a particular URL and save the response in a text file, in a web scraping context?

I would like to post the below data to a particular URL:

choice  yes
high    high
low     low
name    BDNL

To this URL:

https://www.example.com

I would then like to save the response whether HTML or plain text in a text file?

I have experience with Perl and would prefer to use it for the purpose of this task.

Upvotes: 0

Views: 120

Answers (1)

hmatt1
hmatt1

Reputation: 5159

You could use LWP::UserAgent

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use CGI;

my $url = 'http://www.example.com/';

my $ua = LWP::UserAgent->new();
my $response = $ua->post( $url, { choice => "yes", high => "high", low => "low", name => "BDNL" } );
my $content = $response->decoded_content();

my $cgi = CGI->new();
open my $fh, ">", "response.txt" or die "$!";
print $fh $cgi->header() . $content;
close $fh;

Upvotes: 1

Related Questions