Skip to content

Instantly share code, notes, and snippets.

@ace411
Created May 4, 2022 15:09
Show Gist options
  • Save ace411/6b515bbe0eb3d41841a9598c8063cb0d to your computer and use it in GitHub Desktop.
Save ace411/6b515bbe0eb3d41841a9598c8063cb0d to your computer and use it in GitHub Desktop.
Web Scraping with Perl
#!/usr/bin/perl -w
use 5.30.0;
use LWP::UserAgent;
use HTML::TreeBuilder;
use HTML::FormatText;
use HTML::Element;
use Encode;
use warnings;
use open qw(:std :utf8);
if ($#ARGV + 1 != 1) {
die "Please provide song input\n";
}
my $ua = LWP::UserAgent->new;
$ua->agent("Genius Scraper");
my $url = "https://genius.com/$ARGV[0]";
my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 50);
my $root = HTML::TreeBuilder->new();
my $request = $ua->get($url) or die "Cannot contact Genius $!\n";
if ($request->is_success) {
$root->parse(decode_utf8 $request->content);
my $data = $root->look_down(
_tag => "div",
id => "lyrics-root"
);
say $formatter->format($data);
} else {
print "Cannot display the lyrics.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment