In this article, we will learn how to use Elixir and the HTTPoison and Floki 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:
- Import required modules
- Send HTTP request to fetch the Wikipedia page
- Parse the page HTML using Floki
- 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.
Imports
We need these modules:
import HTTPoison
import Floki
Send HTTP Request
To download the web page:
url = "<https://commons.wikimedia.org/wiki/List_of_dog_breeds>"
response = HTTPoison.get!(url, [], hackney: [pool: :default, recv_timeout: 25000])
We make a GET request to the URL.
Parse HTML
To parse the HTML:
html = response.body |> Floki.parse_document!()
The
Find Breed Table
We use a CSS selector to find the table element:
table = html |> Floki.find("table.wikitable.sortable")
This selects the We loop through the rows like this: We iterate through each Inside the loop, we get the column data: We use To download and save images: We reuse the HTTP client and write the image binary to a file. We store the extracted data: The lists can then be processed as needed. And that's it! Here is the full code: This provides a complete Elixir solution using HTTPoison and Floki 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: tag with the required CSS classes.
Iterate Through Rows
Floki.find(table, "tr") |> Enum.each(fn row ->
# Extract data
end)
element within the table. Extract Column Data
[name_cell, group_cell, local_cell, img_cell] = Floki.find(row, "td, th")
name = Floki.text(name_cell) |> String.trim()
group = Floki.text(group_cell) |> String.trim()
local_name = Floki.text(local_cell) |> String.trim()
img = Floki.attribute(img_cell, "src")
Download Images
unless img == nil do
{:ok, image} = HTTPoison.get(img, [], hackney: [pool: :default])
File.write("dog_images/#{name}.jpg", image.body)
end
Store Extracted Data
names = [name | names]
groups = [group | groups]
local_names = [local_name | local_names]
images = [img | images]
# Imports
import HTTPoison
import Floki
# Lists to store data
names = []
groups = []
local_names = []
images = []
# Fetch HTML
url = "<https://commons.wikimedia.org/wiki/List_of_dog_breeds>"
response = HTTPoison.get!(url, [], hackney: [pool: :default, recv_timeout: 25000])
html = response.body |> Floki.parse_document!()
# Find table
table = html |> Floki.find("table.wikitable.sortable")
# Iterate rows
Floki.find(table, "tr") |> Enum.each(fn row ->
[name_cell, group_cell, local_cell, img_cell] = Floki.find(row, "td, th")
name = Floki.text(name_cell) |> String.trim()
group = Floki.text(group_cell) |> String.trim()
local_name = Floki.text(local_cell) |> String.trim()
img = Floki.attribute(img_cell, "src")
unless img == nil do
{:ok, image} = HTTPoison.get(img, [], hackney: [pool: :default])
File.write("dog_images/#{name}.jpg", image.body)
end
names = [name | names]
groups = [group | groups]
local_names = [local_name | local_names]
images = [img | images]
end)
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!