011: Back To Basics - Elixir Setup
Easy guide to setup Elixir in your OS and some exploration on scrapping HTML
Welcome to Snippets of Code, a programming language live journal. The current focus is on the Elixir programming language. Feel free to leave comments or suggestions!
Setting Up Elixir
Elixir is a compelling programming language that is both functional and dynamic, and it's based on the unbeatable Erlang virtual machine (BEAM). With its concurrent and scalable approach, Elixir is the perfect language for building rock-solid applications. If you're new to Elixir and want to get started, our guide is just what you need! We offer a step-by-step approach to installation and configuration on various operating systems, including Mac OS X, Windows, and Linux.
1. Installing Elixir on Mac OS X:
Step 1: Open your preferred web browser and visit the official Elixir website at https://elixir-lang.org/.
Step 2: Navigate to the "Downloads" section and select the appropriate installer for Mac OS X.
Step 3: Double-click on the installer to start the installation process once the installer is downloaded.
Step 4: Follow the on-screen instructions, accept the default settings, and complete the installation.
Step 5: After installation, open the Terminal application and verify the Elixir installation by running the command `elixir --version`. You should see the installed Elixir version displayed in the terminal.
2. Installing Elixir on Windows:
Step 1: Open your preferred web browser and visit the official Elixir website at https://elixir-lang.org/.
Step 2: Navigate to the "Downloads" section and select the appropriate installer for Windows.
Step 3: Double-click on the installer to start the installation process once the installer is downloaded.
Step 4: Follow the on-screen instructions, accept the default settings, and complete the installation.
Step 5: After installation, open the Command Prompt or PowerShell and verify the Elixir installation by running the command `elixir --version`. You should see the installed Elixir version displayed in the command line.
3. Installing Elixir on Linux:
Step 1: Open a terminal on your Linux distribution.
Step 2: Add the Erlang Solutions repository by running the command specific to your Linux distribution. For example, on Ubuntu, you can run:
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb
sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt-get update
Step 3: Install Elixir by running the command `sudo apt-get install elixir`.
Step 4: After the installation completes, verify the Elixir installation by running the command `elixir --version` in the terminal. You should see the installed Elixir version displayed.
Congratulations! You have successfully installed Elixir on your operating system. You can now start exploring the world of Elixir programming and building robust applications.
Web Scraping in Elixir
Now that Elixir is installed on your system, we will delve into a beginner-friendly example of web scraping with Elixir. Web scraping is an automated process of extracting data from websites. Luckily, Elixir has powerful libraries and tools that make web scraping a straightforward task.
To effectively demonstrate web scraping, we will employ the HTTPoison library for executing HTTP requests and the Floki library for HTML parsing. Our program is specifically tailored to retrieve the latest article titles from a designated website.
1. Create a new file called `scraper.exs` (or any name you prefer) and open it in a text editor.
2. Start by adding the following code at the beginning of the file to import the necessary dependencies:
defmodule Scraper do
use HTTPoison.Base
alias Floki
require Logger
end
3. Define a function called `scrape_titles` that takes a URL as a parameter and fetches the HTML content of the page:
defmodule Scraper do
# ...
def scrape_titles(url) do
case HTTPoison.get(url) do
{:ok, %{body: body}} ->
parse_titles(body)
{:error, reason} ->
Logger.error("Failed to fetch URL: #{reason}")
[]
end
end
end
4. Add a helper function called `parse_titles` that uses Floki to extract the titles from the HTML content:
defmodule Scraper do
# ...
defp parse_titles(body) do
html = Floki.parse_html(body)
titles = Floki.find(html, "h2.article-title")
Enum.map(titles, &Floki.text/1)
end
end
5. Finally, add a line to call the `scrape_titles` function with the desired URL and display the results:
defmodule Scraper do
# ...
def run do
url = "https://www.example.com/articles"
titles = scrape_titles(url)
IO.inspect(titles)
end
end
Scraper.run()
Wonderful! Everything's good to go. Simply save the file and head over to your terminal. From there, navigate to the directory where `scraper.exs` is located and enter the `elixir scraper.exs` command. The terminal will then showcase the latest article titles from the given URL.
If you need to tweak the code to fit your needs, go right ahead! You can even try out more advanced techniques like exploring the structure of the website, handling pagination, or gathering extra data. It's all up to you!
If you're looking to automate data extraction, web scraping is a great option. Just remember to respect the website's terms of service and be careful when making requests. Elixir can help you have a smooth and enjoyable web scraping experience!
Resources
Elayne Juten, a software engineer at cars.com, delivered a presentation on the fundamentals of pattern matching. She explained how equal signs can function as match operators instead of assignment operators and demonstrated the practical applications of pattern matching with various data types, such as lists and maps. Juten emphasized the significance of guards in securing successful matches and explored the use of multi-clause functions, case statements, and conditional statements.
Chris McCord delivered a talk that highlighted the potential of Phoenix LiveView, an exciting web technology that has garnered widespread interest across various industries. According to McCord, LiveView is the key to the future of full stack development and could revolutionize the industry. With LiveView, developers can create web applications that feature faster page navigation through a single WebSocket frame over a website connection. During his talk, he also revealed his new position at fly.io, a platform that specializes in hosting web applications, with a particular emphasis on geographical deployment and clustering capabilities.
Adoption of Elixir in production by top companies
The popularity of Elixir, a functional programming language, has been increasing as more top companies and industries worldwide adopt it. Elixir's exceptional productivity, scalability, fault tolerance, and performance make it an ideal language for developing large, fast systems that can handle multiple tasks simultaneously. Companies such as Stord, Allegro Lokalnie, V7, Multiverse.io, Remote.com, and Slab, Mozilla Hubs, and Heroku have incorporated Elixir into their web frameworks for its superior performance, scalability, and ability to handle many users concurrently. Elixir's capacity to simplify large-scale data handling, high concurrency, and fault tolerance has been a valuable asset for various industries, including e-commerce, online gambling, and remote team management.