Skip to content

Instantly share code, notes, and snippets.

@pinkopaque22
Created March 12, 2018 01:12
Show Gist options
  • Save pinkopaque22/85924686e46812be1acc7bcde3a63d1a to your computer and use it in GitHub Desktop.
Save pinkopaque22/85924686e46812be1acc7bcde3a63d1a to your computer and use it in GitHub Desktop.
#require 'open_uri_redirections'
require 'open-uri'
require 'nokogiri'
cities = ['sanfrancisco', 'seattle']
search_terms = ['ruby', 'python', 'react', 'java']
cities.each do |city|
search_terms.each do |search_term|
url = "https://#{city}.craigslist.org/search/cpg?query=#{search_term}&is_paid=all"
document = open(url)
content = document.read
parsed_content = Nokogiri::HTML(content)
puts '========================================================='
puts '- -'
puts "- #{city} - #{search_term} -"
puts '- -'
puts '========================================================='
parsed_content.css('.content').css('.result-row').each do |row|
title = row.css('.hdrlnk').first.inner_text
link = row.css('.hdrlnk').first.attributes["href"].value
posted_at = row.css('time').first.attributes["datetime"].value
hood_elem = row.css('.result-hood')
if hood_elem.any?
neighborhood = hood_elem.first.inner_text.strip
else
neighborhood = ''
end
puts "#{title} #{neighborhood}"
puts "link: #{link}"
puts "Posted at #{posted_at}"
puts '======================================================='
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment