Web scraping is useful to programmatically extract data from websites. Often you need to scrape multiple pages from a site to gather complete information. In this article, we'll see how to scrape multiple pages in Perl using the LWP::UserAgent and HTML::TreeBuilder modules.
Prerequisites
To follow along, you'll need:
use LWP::UserAgent;
use HTML::TreeBuilder;
Define Base URL
—
We'll scrape a blog -
<https://copyblogger.com/blog/>
<https://copyblogger.com/blog/page/2/>
<https://copyblogger.com/blog/page/3/>
Let's define the base URL pattern:
my $base_url = '<https://copyblogger.com/blog/page/%d/>';
The
Specify Number of Pages
Next, we'll specify how many pages to scrape. Let's scrape the first 5 pages:
my $num_pages = 5;
Loop Through Pages
We can now loop from 1 to
for my $page_num (1..$num_pages) {
# Construct page URL
my $url = sprintf($base_url, $page_num);
# Code to scrape each page
}
Send Request and Parse HTML
Inside the loop, we'll send a GET request and parse the HTML:
my $ua = LWP::UserAgent->new;
my $response = $ua->get($url);
if ($response->is_success) {
my $tree = HTML::TreeBuilder->new;
$tree->parse($response->content);
}
This gives us a HTML::TreeBuilder object to extract data from.
Extract Data
Now within the loop we can use XPath queries to extract data from each page:
my @articles = $tree->look_down('_tag'=>'article');
for my $article (@articles) {
# Extract data from article
my $title = $article->look_down('_tag'=>'h2', 'class'=>'entry-title')->as_text;
my $url = $article->look_down('_tag'=>'a', 'class'=>'entry-title-link')->attr('href');
my $author = $article->look_down('_tag'=>'a', 'class'=>'post-author')->as_text;
# Print extracted data
print "Title: $title\\n";
print "URL: $url\\n";
print "Author: $author\\n";
}
Full Code
Our full code to scrape 5 pages is:
use LWP::UserAgent;
use HTML::TreeBuilder;
my $base_url = 'https://copyblogger.com/blog/page/%d/';
my $num_pages = 5;
for my $page_num (1..$num_pages) {
my $url = sprintf($base_url, $page_num);
my $ua = LWP::UserAgent->new;
my $response = $ua->get($url);
if ($response->is_success) {
my $tree = HTML::TreeBuilder->new;
$tree->parse($response->content);
my @articles = $tree->look_down('_tag'=>'article');
for my $article (@articles) {
my $title = $article->look_down('_tag'=>'h2', 'class'=>'entry-title')->as_text;
my $url = $article->look_down('_tag'=>'a', 'class'=>'entry-title-link')->attr('href');
my $author = $article->look_down('_tag'=>'a', 'class'=>'post-author')->as_text;
my @categories = $article->look_down('_tag'=>'a', 'class'=>'entry-category');
my @cat_texts = map { $_->as_text } @categories;
print "Title: $title\n";
print "URL: $url\n";
print "Author: $author\n";
print "Categories: @cat_texts\n";
}
}
}
This allows us to scrape and extract data from multiple pages sequentially in Perl. The code can be extended to scrape any number of pages.
Summary
Web scraping enables collecting large datasets programmatically. With the techniques here, you can scrape and extract information from multiple pages of a website in Perl.
While these examples are great for learning, scraping production-level sites can pose challenges like CAPTCHAs, IP blocks, and bot detection. Rotating proxies and automated CAPTCHA solving can help.
Proxies API offers a simple API for rendering pages with built-in proxy rotation, CAPTCHA solving, and evasion of IP blocks. You can fetch rendered pages in any language without configuring browsers or proxies yourself.
This allows scraping at scale without headaches of IP blocks. Proxies API has a free tier to get started. Check out the API and sign up for an API key to supercharge your web scraping.
With the power of Proxies API combined with Python libraries like Beautiful Soup, you can scrape data at scale without getting blocked.