The New York Park — CLI
Tags: ruby, rails, softwaredevelopment, commandlineinterface
Published: 2020-05-03 17:47:52

---
title: The New York Park — CLI
published: true
date: 2019-12-08 00:00:00 UTC
tags: ruby,rails,softwaredevelopment,commandlineinterface
canonical_url: 
---

### The New York Park — CLI

The New York City Park CLI ( Command Line Application) would scrape [NYC Parks](https://www.nycgovparks.org/) website, to give the user data about different Parks in the selected NYC zip code area. Its build in Ruby, dynamic, open source programming language that’s been used to build some of the awesome sites apps we use each day, like Twitter, Groupon, GitHub, Hulu, Square and Shopify.

![](https://cdn-images-1.medium.com/max/1024/1*3CRRzrZI8HTPd5KlYN8zlA.png)

Not all of the data we might be interested in using to program is available to use through APIs. With Web scraping we are no longer limited to working with APIs and publicly available data sources.

[Ruby](https://www.ruby-lang.org/en) developers use Nokogiri gem as an essential web scraping tool. When combined with OpenURI, a native Ruby library for easily accessing web pages, it becomes a powerful way to get information from websites which otherwise don’t make data available through an API or RSS feed.

![](https://cdn-images-1.medium.com/max/640/0*E2oFf3RPrL-jk3mJ.png)

### So what is Nokogiri?

[Nokogiri](https://nokogiri.org/) is an open source software library to parse HTML and XML in Ruby. It’s actually a ruby gem that will transform a webpage into a ruby object and make all of this web scraping stuff really easy. And ruby gems are optional add-on libraries of code so developers don’t have to reinvent the wheel each time the app with a common use case is build.

**STEP 1** INSTALL NOKOGIRI

The first thing you need to do to use Nokogiri is to install it. You can do so in your terminal with the following code:

gem install nokogiri

**STEP 2** OPENING a Web Page as HTML with Nokogiri and open-uri

Let’s say we have a file, scraper.rb which is responsible for scraping. We need to require Nokogiri and open-uri:

**STEP 3** NOKOGIRI & CSS SELECTORS

CSS-Cascading Style Sheets — describe how HTML elements are to be displayed on screen, paper, or in other media. To scrape you will need to select an element. To select an element simply pass the name of the element you want into the Nokogiri document object’s CSS method.

### How to Choose a CSS Selector

In the following steps i’ll show how do we determine which selector to use to retrieve the desired information.

### FIGURE OUT WHAT YOU WANT TO SCRAPE

Start by figuring out what information you want to scrape. Use the element inspector to find the selector of a certain piece of HTML. In this case, we’ll look the element containing the text in \*\*Facilities \*\*section.

### This is one of the pages I used for NYC Park CLI

![](https://cdn-images-1.medium.com/max/640/0*E1Q0MPNcFIOzd1KI.png)

Hover over an example of the item you’re looking for and with Right click on hit Inspect. This will highlight the corresponding CSS in the Elements window while highlighting the element’s bounds (usually through a colored box).

Click on the item to select its CSS in the Elements window and with Right click on hit Copy and than Copy Selector

![](https://cdn-images-1.medium.com/max/640/0*m04nzc6wz8ZLeRVJ.png)

Nokogiri collects these objects into a hierarchical data structure and allows us to iterate over an array of Nokogiri objects and use enumerators to grab the values of attributes and text.

#### array\_of\_facilities = doc.css("span.park\_facilities\_list\_text")

![](https://cdn-images-1.medium.com/max/640/0*3luQe87INJ6PlcPh.png)

#### Conclusion

By using Nokogiri, we can get any website’s HTML, represented in XML objects, including any text or data displayed on that site.

Scraping require customized code for each site we want to scrape as each website is designed differently.

However, being able to scrape websites gives us access to information that can be time-consuming or otherwise very difficult to collect.

_Originally published at_ [_https://ivanadokic.github.io_](https://ivanadokic.github.io/the_new_york_park_-_cli) _on December 8, 2019._