---
title: Scraping Railscasts
url: https://www.grahambrooks.com/post/2010-04-07-scraping-railscasts/
date: 2010-04-07T00:00:00+00:00
author: Graham Brooks
tags:
- ruby
- Web
---

# Scraping Railscasts

I had a few moments today to get reacquainted with Ruby and Rails programming. It has been a while but I have found time to watch the occasional <a href="http://railscasts.com/">Railscast</a>. I want to watch these videos while on the move but unfortunately the iTunes podcasts are not compatible with the iPad/iPhone. Downloading each one is tedious so I came up with the following:

```ruby
require 'open-uri'
require 'hpricot'

(1..6).each do |page|

  doc = Hpricot(open("http://railscasts.com/episodes?page=#{page}"))

  (doc/"a").each do |l|
    href = l[:href]
    if href =~ /.*ipod_videos.*/ then
      file_name = File.basename(href)

      unless File.exist?(file_name) then
        puts "Downloading #{file_name}"

        File.open(File.basename(href), "w") do |file|
          file.write(open(href).read)
        end
      end
    end
  end
end
```
