How to download all image files from a web page in C++
In this tutorial, we are going to learn how to download all image files from a web page in C++. We will be using an api “wget”. Wget is a C++ program that retrieves content from web servers. Written in C++17 using BSD sockets. You need to install wget on your system to be able to use this API. Whichever compiler you use, put the wget.exe file in the ‘bin’ of the compiler you are using. If you are using codeblocks put the wget.exe in the MinGW/bin folder.
Download all image files from a web page in C++
Wget is a free GNUcommand-line utility tool used to download files from the internet. It retrieves files using HTTP, HTTPS, and FTP.
It serves as a tool to sustain unstable and slow network connections. If a network problem occurs during a download, this helpful software can resume retrieving the files without starting from scratch.
-p command is used to save file to a specific location.
-ndĀ option is used to specify that wget should not create a directory hierarchy for downloaded files.
-r option is used to recursively download files from a specified URL
We first take URL from which we want to download.
So, this code below will download jpeg, jpg, bmp, gif, and png files present on the page.
#include <iostream> #include <string> using namespace std; int main() { string url = "https://unsplash.com/s/photos/jpg"; string cmd = "wget -nd -r -P ./images -A jpeg,jpg,bmp,gif,png " + url; system(cmd.c_str()); return 0; }
Wget will send HTTP requests to URL and save the files in the images folder.
Output:
Leave a Reply