How to Extract the Title From an HTML Page with Ruby
This snippet will make a request to this page and extract the title from the title tag.
require 'open-uri'
html = open('http://www.seanbehan.com/how-to-extract-the-title-from-an-html-page-with-ruby').read
title = html.match(/(.*)<\/title>/) { $1 } puts title
The regular expression here matches everything between the title tags. Anything within the parens "(.*)" is kept around as variable which you can access in the block with variables $1, $2, $3... $n, depending on how many matches are found.
Comments