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 Elixir using the HTTPoison and Floki libraries.
Prerequisites
To follow along, you'll need:
def deps do
[
{:httpoison, "~> 1.8"},
{:floki, "~> 0.30.0"}
]
end
Import Libraries
We'll need the following imports:
import HTTPoison
import Floki
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:
base_url = "<https://copyblogger.com/blog/page/#{page}/>"
The
Specify Number of Pages
Next, we'll specify how many pages to scrape. Let's scrape the first 5 pages:
num_pages = 5
Loop Through Pages
We can now loop from 1 to
for page <- 1..num_pages do
# Construct URL
url = String.replace(base_url, "#{page}", to_string(page))
# Code to scrape each page
end
Send Request and Parse HTML
Inside the loop, we'll send a GET request and parse the HTML:
{:ok, response} = HTTPoison.get(url)
html = response.body |> Floki.parse_document
This gives us a parsed HTML document to extract data from.
Extract Data
Now within the loop we can use Floki selectors to find and extract data from each page:
html
|> Floki.find("article")
|> Enum.each(fn(article) ->
# Extract data from article tag
title = Floki.find(article, "h2.entry-title") |> Floki.text
url = Floki.find(article, "a.entry-title-link") |> Floki.attribute("href")
author = Floki.find(article, "div.post-author a") |> Floki.text
end)
Full Code
Our full code to scrape 5 pages is:
import HTTPoison
import Floki
base_url = "https://copyblogger.com/blog/page/#{page}/"
num_pages = 5
for page <- 1..num_pages do
url = String.replace(base_url, "#{page}", to_string(page))
{:ok, response} = HTTPoison.get(url)
html = response.body |> Floki.parse_document
html
|> Floki.find("article")
|> Enum.each(fn(article) ->
title = Floki.find(article, "h2.entry-title") |> Floki.text
url = Floki.find(article, "a.entry-title-link") |> Floki.attribute("href")
author = Floki.find(article, "div.post-author a") |> Floki.text
categories = Floki.find(article, "div.entry-categories a") |> Enum.map(&Floki.text/1)
IO.puts "Title: #{title}"
IO.puts "URL: #{url}"
IO.puts "Author: #{author}"
IO.puts "Categories: #{inspect(categories)}"
end)
end
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 Elixir.
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.