<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nazli Ander on Medium]]></title>
        <description><![CDATA[Stories by Nazli Ander on Medium]]></description>
        <link>https://medium.com/@nazliander?source=rss-3c6a28ba9bdd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*gprkp4fS5vCrMB0nQU-llA.jpeg</url>
            <title>Stories by Nazli Ander on Medium</title>
            <link>https://medium.com/@nazliander?source=rss-3c6a28ba9bdd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 01:59:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nazliander/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Scraping and Storing Crypto-currency Prices with Scala and PostgreSQL]]></title>
            <link>https://itnext.io/scraping-and-storing-crypto-currency-prices-with-scala-and-postgresql-a6bb86a66f74?source=rss-3c6a28ba9bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6bb86a66f74</guid>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Nazli Ander]]></dc:creator>
            <pubDate>Mon, 26 Aug 2019 19:35:39 GMT</pubDate>
            <atom:updated>2019-08-27T18:07:12.590Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>Web scraping mostly involves text-intensive tasks such as product review scraping, gathering real-estate listings or even tracking online reputation and presence. When one application scrapes only String data types for qualitative analysis, it may not need type safety. However, in case the end goal of the web scraping is to do quantitative analysis with prices or weather forecasts, using a type-safe language might be quite handy.</p><p>In this article, we aim to give a small and interesting example of price scraping for crypto-currencies by using Scala and storing those into a PostgreSQL database. To scrape the prices we selected to use CoinMarketCap <a href="https://coinmarketcap.com/">homepage</a>. It is a crypto-currency knowledge website, which gives information also on market capitalizations (relative market sizes), circulating supply and trading volumes. Even though it is fascinating to see all those information together, to keep it simple we will be only scraping the prices.</p><p>This article might be considered as a tutorial, and it requires a basic level of knowledge of docker-compose and Scala.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XIv7wtoBBxf95mezwstG6Q.jpeg" /><figcaption>A summary of the pipeline that this tutorial uses</figcaption></figure><p><strong>Tools and Steps</strong></p><p>While web scraping in Scala, we will be using an HTML parsing library called scala-scraper with <strong>JSoup</strong>. Following that, we will be inserting the scraped prices to the PostgreSQL database by using a functional JDBC tool called <strong>doobie</strong>.</p><p>Although we mentioned some fancy library and tool names, the real magic happens in case classes. For each call to the CoinMarketCap homepage, we aim to retrieve the long crypto-currency table with type safety. To do that we created CoinCreate and CoinInsert case classes and companion objects.</p><p>We will start explaining first the case classes together with their companion objects, as we aimed to model the data while creating those. Then we will explain the simple functions for retrieving the updated price table from the homepage. Lastly, we will explain how we inserted the table records into the PostgreSQL database running locally. We can power the database with this simple <a href="https://github.com/nazliander/scrape-insert-postgresql/blob/master/simple-tutorial/docker-compose-setup/docker-compose.yml">docker-compose file</a>. In the docker-compose file, we initialized a PostgreSQL database with a name dev, username admin, and a password as admin.</p><p>The steps that are explained in this tutorial are displayed in the pipeline above.</p><p><strong>Case Classes and Companion Objects</strong></p><p>Although there might be different approaches to model the data, we can start by creating two case classes as <em>CoinCreate</em> and <em>CoinInsert</em>. Those will help us to keep the data types safe while scraping the price table and inserting into a database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QEZWeKAqcfOVbnDkQtHUFg.png" /><figcaption>A view from the CoinMarketCap homepage prices table</figcaption></figure><p>CoinCreate aims to safely type a pair of crypto-currency code and its current price. Thus, it has two parameters code(referring to the currency code) and price(current price in USD). However, while thinking about its companion object we need to consider the shape of the price records in each row. For instance, if we consider only to use coin names and prices in our case class, in an array of records their indices will be 1 and 3. This is quite similar to column indices for tables.</p><p>By observing the price table (above you can find a screenshot from the homepage), we decide to use a companion object to have an apply method for functionally transforming an input of String List to CoinCreate. Although this transformation is not that straightforward, we can use helper functions to get only the coin code (getCoinCode) and transform the dollar price string into a double (numberStringToDouble).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a7917916a49cd46de9080769902f22e/href">https://medium.com/media/7a7917916a49cd46de9080769902f22e/href</a></iframe><p>CoinInsert aims to safely type a pair of crypto-currency code, its current price, and a log timestamp for insertion time logging. We can use this case class while inserting a vector of CoinCreate into PostgreSQL. As its parameters are so similar to CoinCreate, we can create a simple companion object to transform a CoinCreate to CoinInsert. This object’s apply method can naturally add the current timestamp to a CoinCreate to obtain a CoinInsert.</p><p>Hence the only difference between a CoinCreate case class and CoinInsert case class will be the current Timestamp , notated as a logTimestamp parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1347b8e05cca9328bf70933d9110366/href">https://medium.com/media/a1347b8e05cca9328bf70933d9110366/href</a></iframe><p><strong>Scraping Functions</strong></p><p>Scraping with <strong>scala-scraper</strong> and <strong>JSoup</strong> is quite easy. First, we need to GET request to the homepage by creating a new <strong>JSoup</strong> browser. A new <strong>JSoup</strong> browser enables to fetch HTML from the web. Since we need only HTML parsing <strong>JSoup</strong> was enough in this case, for Javascript using pages other browser options could be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/917087afaace57a19427c4a8fd48a73e/href">https://medium.com/media/917087afaace57a19427c4a8fd48a73e/href</a></iframe><p>By using the GET request, we need to find the main table and store it as a Vector of Strings. Luckily when we specify that we are looking for a table element, <strong>scala-scraper</strong>’s table method does all the job for us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88d5313d9b150c0080af85f1bba56246/href">https://medium.com/media/88d5313d9b150c0080af85f1bba56246/href</a></iframe><p>Lastly, we need to slice the Vector, starting from the second index till the last lines, as the first line contains column names (headers). The resulting sliced Vector would still have the table rows with their HTML elements as String. So we can benefit from functional programming to map all the table rows (Vector elements) while extracting text in the elements then transform to CoinCreate (the comfort of having a tailor-made apply function).</p><p><strong>Insertion Functions</strong></p><p><strong>doobie</strong> is an amazing functional JDBC layer for Scala. It provides a functional way to write any JDBC program. In this tutorial, we will keep it quite simple by writing only a connection Transactor to connect to the local PostgreSQL database and an insertion function to make the transactions with type-safety.</p><p>To connect to the database, we need to use a Transactor stating the type of the driver (in our case it is a PostgreSQL driver), URL for connection, user name, password, and an Execution Context (EC). The transactor needs an implicit val to define the EC. For non-blocking operations<strong> doobie</strong>’s Transactor uses contextShift. For testing our code <strong>doobie</strong> documentation recommends using synchronous EC.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b7ae4a4db1447a9e769c68b0cb291a4/href">https://medium.com/media/0b7ae4a4db1447a9e769c68b0cb291a4/href</a></iframe><p>For writing a row by row insertion function we can use SQL interpolation. The function has an input of CoinInsert and an output of Update0 (representing a parameterized statement where the arguments are known).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1510aad73d1f17b9743325acfff56fcc/href">https://medium.com/media/1510aad73d1f17b9743325acfff56fcc/href</a></iframe><p>Lastly, we can GET request the homepage by using the getCoinUpdatedTable function. This will return a Vector of Strings.</p><p>Consequently, we can use this Vector (coinTable) to transform CoinCreate to CoinInsert case class and execute the insert statement we prepared in the previous step.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97459ed6c224dc219dbb2e5af15f64e8/href">https://medium.com/media/97459ed6c224dc219dbb2e5af15f64e8/href</a></iframe><p><strong>Last Words</strong></p><p>Thanks to <strong>doobie</strong>, with only a few lines we were able to scrape the crypto-currency prices from CoinMarketCap and insert into a local PostgreSQL database. Although the code does its job, for now, the source code can be extended with exception handling and monitoring. The whole main class can be found below. And you can find the whole project in <a href="https://github.com/nazliander/scrape-insert-postgresql/tree/master/simple-tutorial">this</a> Github repository.</p><p>With this short article, we aimed to introduce some Scala concepts to web scraping. To learn more on data engineering one can choose to attend <a href="https://www.linkit.nl/vacatures/data-engineering/0007529/data-engineering-apprenticeship-starts-feb-1st-2020">a Bootcamp</a> with hands-on training, do more projects, read and share more.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e86fe513505c20910d996b2d4356422e/href">https://medium.com/media/e86fe513505c20910d996b2d4356422e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6bb86a66f74" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/scraping-and-storing-crypto-currency-prices-with-scala-and-postgresql-a6bb86a66f74">Scraping and Storing Crypto-currency Prices with Scala and PostgreSQL</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>