2016-11-08 440 views
1

我有這樣的HTML:如何通過嵌套項目迭代

<div class="date"> 
    <h3 class="date-title">Today</h3> 

    <div class="film"> 
    <img class="poster" src="film1" /> 
     <h4 class="title">Film 1</h4> 
    <ul class="session-times"> 
     <li> 
     <a href="#"> 
      <time>12:00 PM</time> 
     </a> 
     </li> 
    </ul> 
    </div><!-- /.film --> 

    <div class="film"> 
    <img class="poster" src="film2" /> 
     <h4 class="title">Film 2</h4> 
    <ul class="session-times"> 
     <li> 
     <a href="#"> 
      <time>3:00 PM</time> 
     </a> 
     </li> 
    </ul> 
    </div><!-- /.film --> 

    <div class="film"> 
    <img class="poster" src="film3" /> 
     <h4 class="title">Film 3</h4> 
    <ul class="session-times"> 
     <li> 
     <a href="#"> 
      <time>6:00 PM</time> 
     </a> 
     </li> 
    </ul> 
    </div><!-- /.film --> 
</div><!-- /.date --> 

<div class="date"> 
    <h3 class="date-title">Tomorrow</h3> 

    <div class="film"> 
    <img class="poster" src="film1" /> 
     <h4 class="title">Film 1</h4> 
    <ul class="session-times"> 
     <li> 
     <a href="#"> 
      <time>2:00 PM</time> 
     </a> 
     </li> 
    </ul> 
    </div><!-- /.film --> 

    <div class="film"> 
    <img class="poster" src="film2" /> 
     <h4 class="title">Film 2</h4> 
    <ul class="session-times"> 
     <li> 
     <a href="#"> 
      <time>5:00 PM</time> 
     </a> 
     </li> 
    </ul> 
    </div><!-- /.film --> 

    <div class="film"> 
    <img class="poster" src="film3" /> 
     <h4 class="title">Film 3</h4> 
    <ul class="session-times"> 
     <li> 
     <a href="#"> 
      <time>8:00 PM</time> 
     </a> 
     </li> 
    </ul> 
    </div><!-- /.film --> 
</div><!-- /.date --> 

,我使用這個Ruby代碼中提取數據:

這給了我:

[ 
    { 
    "date": "Today" 
    }, 
    { 
    "date": "Tomorrow" 
    } 
] 

但我想循環播放各個.film部分中的n三部電影,並將它們包含在o輸出,所以它應該看起來更像這樣:

[ 
    { 
    "Today": { 
     "films": [ 
     { 
      "film": "Film1", 
      "time": "12:00 PM" 
     }, 
     { 
      "film": "Film2", 
      "time": "15:00 PM" 
     }, 
     { 
      "film": "Film3", 
      "time": "6:00 PM" 
     } 
     ] 
    }, 
    { 
    "Tomorrow": { 
     "films": [ 
     { 
      "film": "Film1", 
      "time": "14:00 PM" 
     }, 
     { 
      "film": "Film2", 
      "time": "5:00 PM" 
     }, 
     { 
      "film": "Film3", 
      "time": "8:00 PM" 
     } 
     ] 
    }, 

我找不出在哪裏建立我的數組嵌套循環。

回答

1

這裏的想法是首先找到類date(Nokogiri節點數組)的節點。並在你想要的結構中轉換這個數組(使用map方法)。結果將是散列的數組(因爲map)(因爲我在外部map中返回)。要在任何散列中創建所需的結構,我使用相同的概念:使用css方法和map找到所需的nokogiri節點。

date_nodes = nokogiri_object.css('.date') 
date_nodes.map do |date| 
    { 
    date.css('.date-title').text => { 
     "films" => date.css('.film').map do |film| 
     { 
      "film" => film.css('img.poster').attr('src').value, 
      "time" => film.css('time').text 
     } 
     end 
    } 
    } 
end 
=> [{"Today"=>{ 
    "films"=>[ 
    {"film"=>"film1", "time"=>"12:00 PM"}, 
    {"film"=>"film2", "time"=>"3:00 PM"}, 
    {"film"=>"film3", "time"=>"6:00 PM"}]}}, 
    {"Tomorrow"=>{ 
    "films"=>[ 
    {"film"=>"film1", "time"=>"2:00 PM"}, 
    {"film"=>"film2", "time"=>"5:00 PM"}, 
    {"film"=>"film3", "time"=>"8:00 PM"}]}} 
    ] 
+0

太好了,謝謝! – matski

+2

而不是折騰代碼,也提供了代碼的作用的解釋。我們的想法是教育,而不僅僅是提供解決方案。這是給某人一條魚或教他們釣魚的區別。 –

+0

我不認爲作者無法理解我的代碼。我認爲對於初學者/非專家來說,通常很難想出如何解決問題,但向他們展示解決方案並不意味着他們無法理解解決方案。 – Ursus