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 C++ using the cpp-netlib and cppxpath libraries.
Prerequisites
To follow along, you'll need:
Include Libraries
We'll need the following header files:
#include <netlib.http>
#include <pugixml.hpp>
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:
std::string baseUrl = "<https://copyblogger.com/blog/page/{}/>";
The
Specify Number of Pages
Next, we'll specify how many pages to scrape. Let's scrape the first 5 pages:
int numPages = 5;
Loop Through Pages
We can now loop from 1 to
for (int page = 1; page <= numPages; page++) {
// Construct page URL
std::string url = boost::str(boost::format(baseUrl) % page);
// Code to scrape each page
}
Send Request and Parse HTML
Inside the loop, we'll send a GET request and parse the HTML:
netlib::http::client client;
netlib::http::client::request request(url);
auto response = client.get(request);
pugi::xml_document doc;
doc.load(response.body());
This gives us an XML document to extract data from.
Extract Data
Now within the loop we can use
For example, to get all article elements:
auto articles = doc.select_nodes("//article");
We can loop through
Full Code
Our full code to scrape 5 pages is:
#include <netlib.http>
#include <pugixml.hpp>
int main() {
std::string baseUrl = "<https://copyblogger.com/blog/page/{}/>";
int numPages = 5;
for (int page = 1; page <= numPages; page++) {
std::string url = boost::str(boost::format(baseUrl) % page);
netlib::http::client client;
netlib::http::client::request request(url);
auto response = client.get(request);
pugi::xml_document doc;
doc.load(response.body());
auto articles = doc.select_nodes("//article");
for (auto& article : articles) {
// Extract data from article
auto title = article.select_node("./h2[@class='entry-title']").node().text();
auto url = article.select_node("./a[@class='entry-title-link']/@href").node().value();
auto author = article.select_node("./div[@class='post-author']/a").node().text();
std::vector<std::string> categories;
for (auto& cat : article.select_nodes("./div[@class='entry-categories']/a")) {
categories.push_back(cat.node().text());
}
// Print extracted data
std::cout << "Title: " << title << "\\n";
std::cout << "URL: " << url << "\\n";
std::cout << "Author: " << author << "\\n";
for (auto& cat : categories) {
std::cout << "Category: " << cat << "\\n";
}
std::cout << "\\n";
}
}
}
This allows us to scrape and extract data from multiple pages sequentially. 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 C++.
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.