0

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

0 回答 0