Created
December 20, 2011 10:00
-
-
Save runeb/1501016 to your computer and use it in GitHub Desktop.
Finn.no real estate pages as JSON data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
require 'open-uri' | |
require 'uri' | |
def finn(id) | |
doc = open("http://www.finn.no/finn/realestate/homes/object?finnkode=#{id}").read | |
doc =~ /helios_parameters\s?:\s?'(.*)'/ | |
attrs = $1.split(';').inject({}) do |mem, e| | |
key, value = e.split "=" | |
value = URI.unescape(value) | |
if key !~ /bilde/ | |
value = value.gsub('_', ' ') | |
case key | |
when /mobil$/ | |
value.gsub!(" ", "") | |
end | |
mem[key] = URI.unescape(value) | |
end | |
mem | |
end | |
attrs['geo'] = { } | |
doc =~ /utmx : "(\d+)"/ | |
attrs['geo']['x'] = $1 | |
doc =~ /utmy : "(\d+)"/ | |
attrs['geo']['y'] = $1 | |
if attrs['sold'] = !!(doc =~ /soldNoteHolder/) | |
doc =~ /soldNoteHolder.*<p>\((.*),-\)/m | |
attrs['sold_price'] = $1 | |
end | |
doc = Nokogiri::HTML.parse doc | |
attrs['images'] = doc.css('.gallery-image').map {|i| i.attribute('data-main').text} | |
attrs | |
end | |
require 'nokogiri' | |
def today | |
doc = Nokogiri::HTML.parse(open('http://www.finn.no/finn/realestate/homes/result?page=1&periode=1').read) | |
page = 1 | |
links = [] | |
while(true) | |
new_links = doc.css('ul#resultlist li').map do |li| | |
li.css('a').attribute('href').to_s.split("=")[-1] | |
end | |
if new_links.size == 0 | |
break | |
end | |
links += new_links | |
puts "Have #{links.size} links now" | |
page += 1 | |
doc = Nokogiri::HTML.parse(open("http://www.finn.no/finn/realestate/homes/result?page=#{page}&periode=1").read) | |
end | |
links | |
end | |
require 'pp' | |
require 'json' | |
puts finn('31233480').to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment