私は nokogiri を使用していくつかの html を解析し、ページ上のすべての結果を提供しています。最終的には次のようなハッシュに設定したい
:address => "xxx"
:unit => "xx"
:url => "www.xxx.com"
:price => "$xxx
今私のコードは次のようになります
def initialize(type, num)
@type = type
@url = "http://xxxx.com/ny/#{type}/location/status:open?page=# {num}&sort_by=price_desc"
@num = num
@nodes = Nokogiri::HTML(open(@url + @type + @num))
end
def summary
listing_data = @nodes
hash = ["listing_class",listing_data.css(
'div#content div.content_inner div.section
div.supercolumn
div.column
div.block
div.items
div#pages
div.unsponsored
div.item.compact
div.item_inner
div.body
h3
a').text,
"address",
listing_data.css(
'div#content
div.content_inner
div.section
div.supercolumn
div.column
div.block
div.items
div#pages
div.unsponsored
div.item.compact
div.item_inner
div.body
h3
a').text,
"unit",
listing_data.css(
'div#content
div.content_inner
div.section
div.supercolumn
div.column
div.block
div.items
div#pages
div.unsponsored
div.item.compact
div.item_inner
div.body
h3
a').text,
"price",
listing_data.css(
'div#content
div.content_inner
div.section
div.supercolumn
div.column
div.block
div.items
div#pages
div.unsponsored
div.item.compact
div.item_inner
div.more
h3
span').text]
a = Hash[*hash]
end
rentals = Listing.new("rentals", "1")
puts rentals.summary
end
私が抱えている問題は、出力がすべてごちゃ混ぜになっていて、コンマで区切られていないことです。
どんな提案でも大歓迎です。
ああ、出力は次のようになります-
{"listing_class"=>["156 Sullivan Street192 Sixth Avenue77 Sullivan Street196 Sixth Avenue77 Sullivan535 Broadway67 Thompson Street145 Sixth Avenue"], "address"=>"156 Sullivan Street192 Sixth Avenue77 Sullivan Street196 Sixth Avenue77 Sullivan535 Broadway67 Thompson Street145 Sixth Avenue481 Greenwich Street109 Mercer Street", "unit"=>"156 Sullivan Street192 Sixth Avenue77 Sullivan Street196 Sixth Avenue77 Sullivan535 Broadway67 Thompson Street145 Sixth Avenue481 Greenwich Street109 Mercer Street", "price"=>"$4,295$2,550$5,500$4,300$5,600$10,000(NO FEE)$2,300$"}