In this article, we will learn how to use C++ and the cpp-httplib and cpp-selector libraries to download all the images from a Wikipedia page.
—-
Overview
The goal is to extract the names, breed groups, local names, and image URLs for all dog breeds listed on this Wikipedia page. We will store the image URLs, download the images and save them to a local folder.
Here are the key steps we will cover:
- Include required headers
- Send HTTP request to fetch the Wikipedia page
- Parse the page HTML using cpp-selector
- Find the table with dog breed data using a CSS selector
- Iterate through the table rows
- Extract data from each column
- Download images and save locally
- Print/process extracted data
Let's go through each of these steps in detail.
Includes
We need these headers:
#include <httplib.h>
#include <selector/selector.h>
#include <fstream>
Send HTTP Request
To download the web page:
httplib::Client cli("commons.wikimedia.org");
auto res = cli.Get("/wiki/List_of_dog_breeds",
{{"User-Agent", "cpp-httplib"}});
if(res) {
// Parse HTML
}
We make a GET request and provide a custom user-agent.
Parse HTML
To parse the HTML:
pugi::xml_document doc;
doc.load(res->body.c_str());
auto html = doc.child("html");
The
Find Breed Table
We use a CSS selector to find the table element:
auto table = html.select_node("table.wikitable.sortable").node();
This selects the We loop through the rows: We iterate through Inside the loop, we get the column data: We use To download and save images: We reuse the HTTP client and write image bytes to a file. We store the extracted data: The vectors can then be processed as needed. And that's it! Here is the full code: This provides a complete C++ solution using cpp-httplib and cpp-selector to scrape data and images from HTML tables. The same approach can apply to many websites. 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.
Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you
curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com" <!doctype html> Enter your email below to claim your free API key: with the required CSS classes.
Iterate Through Rows
for (auto& row : table.select_nodes("tr")) {
// Extract data
}
elements within the table. Extract Column Data
auto cells = row.select_nodes("td, th");
auto name = cells[0].child("a").text().get();
auto group = cells[1].text().get();
auto localNameNode = cells[2].select_node("span");
auto localName = localNameNode.text().get("");
auto img = cells[3].select_node("img");
auto photograph = img.attribute("src").value();
Download Images
if (!photograph.empty()) {
auto img_data = cli.Get(photograph.c_str());
std::ofstream file("dog_images/" + name + ".jpg", std::ios::binary);
file << img_data->body;
}
Store Extracted Data
names.push_back(name);
groups.push_back(group);
localNames.push_back(localName);
photographs.push_back(photograph);
// Includes
#include <httplib.h>
#include <selector/selector.h>
#include <fstream>
#include <vector>
// Vectors to store data
std::vector<std::string> names;
std::vector<std::string> groups;
std::vector<std::string> localNames;
std::vector<std::string> photographs;
// HTTP client
httplib::Client cli("commons.wikimedia.org");
// Send request
auto res = cli.Get("/wiki/List_of_dog_breeds",
{{"User-Agent", "cpp-httplib"}});
if(res) {
// Parse HTML
pugi::xml_document doc;
doc.load(res->body.c_str());
auto html = doc.child("html");
// Find table
auto table = html.select_node("table.wikitable.sortable").node();
// Iterate rows
for (auto& row : table.select_nodes("tr")) {
// Get cells
auto cells = row.select_nodes("td, th");
// Extract data
auto name = cells[0].child("a").text().get();
auto group = cells[1].text().get();
auto localNameNode = cells[2].select_node("span");
auto localName = localNameNode.text().get("");
auto img = cells[3].select_node("img");
auto photograph = img.attribute("src").value();
// Download image
if (!photograph.empty()) {
auto img_data = cli.Get(photograph.c_str());
std::ofstream file("dog_images/" + name + ".jpg", std::ios::binary);
file << img_data->body;
}
// Store data
names.push_back(name);
groups.push_back(group);
localNames.push_back(localName);
photographs.push_back(photograph);
}
}
Browse by tags:
Browse by language:
The easiest way to do Web Scraping
Try ProxiesAPI for free
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
...Don't leave just yet!