how to parsing HTML with Nokogiri


Installation is very easy. Just add to your Gemfile.

gem "nokogiri"

Learn how to Generate HTML.

Quick start to parsing HTML

Parsing HTML is easy, and you can take advantage of CSS selectors or XPath queries to find things in your document:

require 'open-uri'
require 'nokogiri'

# Perform a google search
doc = Nokogiri::HTML(open(''))

# Print out each link using a CSS selector
doc.css('h3.r > a.l').each do |link|
  puts link.content

Here is an example parsing some HTML and searching it using a combination of CSS selectors and XPath selectors:

require 'nokogiri'

doc = Nokogiri::HTML.parse(<<-eohtml)
    <title>Hello World</title>
    <h1>This is an awesome document</h1>
      I am a paragraph
        <a href="">I am a link</a>

# Search for nodes by css
doc.css('p > a').each do |a_tag|
  puts a_tag.content

# Search for nodes by xpath
doc.xpath('//p/a').each do |a_tag|
  puts a_tag.content

# Or mix and match.'//p/a', 'p > a').each do |a_tag|
  puts a_tag.content

# Find attributes and their values'a').first['href']

Set Up SSH Keys

We use SSH keys to establish a secure connection between your computer and GitHub. Setting them up is fairly easy, but does involve a number of steps.

To make sure you generate a brand new key, you need to check if one already exists. First, you need to open an app called Terminal.
1. First, check for existing ssh keys on your computer:

cd ~/.ssh

2. Backup and remove existing SSH keys. Since there is already an SSH directory you’ll want to back the old one up and remove it:

$ ls
$ mkdir key_backup
$ cp id_rsa* key_backup
$ rm id_rsa*

3. Generate a new SSH key. To generate a new SSH key, enter the code below. We want the default settings so when asked to enter a file in which to save the key, just press enter.

$ ssh-keygen -t rsa -C ""

Now you need to enter a passphrase.

Enter passphrase (empty for no passphrase):
Enter same passphrase again: