我正在使用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$"}