1

所以我使用 gem 文件从 reddit 的首页拉取链接。gem 函数返回一个哈希,其中包含首页上的所有链接及其所有信息(评论数、业力、作者、日期、链接、上升、下降等)。以下是包含我首页上第一个链接信息的哈希的乞求:

{"modhash"=>"ubekfji9tr491ba98806d33ec78693dc157579335eb1ab283b", "children"=>[{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>无,“media_embed”=>{},“subreddit”=>“图片”,“selftext_html”=>无,“selftext”=>“”,“喜欢”=>无,“link_flair_text”=>无,“id "=>"1dt29e", "clicked"=>false, "title"=>"我们在意大利的水管工亲自出去找我们帮忙做他的英语作业 :) 希望他能通过他的期末考试!", "media" =>nil, "score"=>3700, "approved_by"=>nil, "over_18"=>false, "hidden"=>false,“缩略图”=>“ http://b.thumbs.redditmedia.com/Fdid4luCfXah0bQ7.jpg", "subreddit_id"=>"t5_2qh0u", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>17890, "saved"=>false, "is_self"= >false, "permalink"=>"/r/pics/comments/1dt29e/our_plumber_in_italy_put_himself_out_there_to_ask/", "name"=>"t3_1dt29e", "created"=>1367893951.0, "url"=>" http://i. imgur.com/3xn1c8s.jpg ", "author_flair_text"=>nil, "author"=>"Junpha", "created_utc"=>1367865151.0, "distinguished"=>nil, "num_comments"=>553, "num_reports"= >nil, "ups"=>21590}},

该结尾,将下一个链接分开,如下所示:

{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"有趣的", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt4xv", "clicked"=>false, "title "=>"多么美丽,雄伟....哎呀", "media"=>nil, "score"=>2848, "approved_by"=>nil, "over_18"=>false, "hidden"=>false , "缩略图"=>" http://a.thumbs.redditmedia.com/vkstE1PFr9iyYpux.jpg ", "subreddit_id"=>"t5_2qh33", "edited"=>false,"link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>8128, "saved"=>false, "is_self"=>false, "permalink"=>"/r/funny/comments/1dt4xv/ what_a_beautiful_majestic_oops/", "name"=>"t3_1dt4xv", "created"=>1367895955.0, "url"=>" http://i.imgur.com/62q1jWN.gifgifgif", "author_flair_text"=>nil, "author"=>"themisc", "created_utc"=>1367867155.0, "distinguished"=>nil, "num_comments"=>279, "num_reports"=>nil, "ups"= >10976}},

我基本上需要某种方式来解析这个哈希的每个条目,以便我可以将所有相关的东西存储到一个数组中。真的,我只想将"title"and"url"部分存储到一个数组中。我试过这样的事情:

@red.get_listing().each do |x['title']|
    puts x
end

但我意识到这是一个散列的散列..所以我对如何解析这个有点迷茫。

4

3 回答 3

2
hash[:children].each do |child|
  puts child[:title]
  puts child[:url]
end

这有帮助吗?

于 2013-05-06T23:48:54.303 回答
1
hash = {"modhash"=>"ubekfji9tr491ba98806d33ec78693dc157579335eb1ab283b", 
"children"=>[{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"pics", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt29e", "clicked"=>false, "title"=>"Our plumber in Italy put himself out there to ask us for help on his English homework :) Hope he passes his finals!", "media"=>nil, "score"=>3700, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://b.thumbs.redditmedia.com/Fdid4luCfXah0bQ7.jpg", "subreddit_id"=>"t5_2qh0u", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>17890, "saved"=>false, "is_self"=>false, "permalink"=>"/r/pics/comments/1dt29e/our_plumber_in_italy_put_himself_out_there_to_ask/", "name"=>"t3_1dt29e", "created"=>1367893951.0, "url"=>"http://i.imgur.com/3xn1c8s.jpg", "author_flair_text"=>nil, "author"=>"Junpha", "created_utc"=>1367865151.0, "distinguished"=>nil, "num_comments"=>553, "num_reports"=>nil, "ups"=>21590}},{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"funny", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt4xv", "clicked"=>false, "title"=>"What a beautiful, majestic.... oops", "media"=>nil, "score"=>2848, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://a.thumbs.redditmedia.com/vkstE1PFr9iyYpux.jpg", "subreddit_id"=>"t5_2qh33", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>8128, "saved"=>false, "is_self"=>false, "permalink"=>"/r/funny/comments/1dt4xv/what_a_beautiful_majestic_oops/", "name"=>"t3_1dt4xv", "created"=>1367895955.0, "url"=>"http://i.imgur.com/62q1jWN.gif", "author_flair_text"=>nil, "author"=>"themisc", "created_utc"=>1367867155.0, "distinguished"=>nil, "num_comments"=>279, "num_reports"=>nil, "ups"=>10976}}

]}

hash['children'].map { |child| child['data']['url'] }
hash['children'].map { |child| child['data']['title'] }

或者如果您想使用符号(并且在导轨中)

hash.with_indifferent_access[:children].first[:data][:title]
于 2013-05-07T01:10:02.287 回答
0

你真的不需要解析它们,只需访问它的值:

hash[:data][:title]
hash[:data][:url]
于 2013-05-06T23:57:54.647 回答