Craigslist Command Line Search Using Ruby

Posted by Tully on Mon 02 December 2013

One of my friends keeps looking through apartments on Craigslist. I figured it would be easier to just do quick searches through the command line.

I built a simple Ruby command line script for searching apartments. It takes a single argument, the search query. I have hardcoded some values such as hasPic, maxAsk, minAsk, and a few others. If you want to use this, these can easily be changed by updating the scripts values.

require 'open-uri'
require 'mechanize'
require 'colorize'

unless ARGV[0]
    puts 'Usage: ./craigslist.rb <query>'

query = ARGV[0]
craigslist_url = ''

url = []
url << ""
url << "?catAbb=hhh"
url << "&query=#{query}"
url << "&zoomToPosting="
url << "&minAsk=800"
url << "&maxAsk=1400"
url << "&hasPic=1"
url = url.join

mech = { |agent|
    agent.user_agent_alias = 'Mac Safari'

mech.get(url) do |page|"//p[@class='row']").each do |row|
        link ='a')[0]
        link_href = /^http/ =~ link['href'] ? link['href'] : craigslist_url + link['href']
        link_text ='a')[1].text
        city ="small").text
        price ="span[@class='price']")[0].text

        puts "#{} #{city} - #{}\n#{link_href.cyan}\n"


./craigslist.rb LOCATION


./craigslist.rb "santa monica"