如何遍历嵌套项



我有这个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代码提取数据:

nokogiri_object.css('.date').each do |d|
  date = d.css('.date-title').text
  dates.push(date: date)
  d.css('.film').each do |film|
    title = film.css('.title')
    title_en = title.text.strip
    time = film.css('.session-times/li/a/time').text
  end
end

这给了我:

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

但是我想在每个.film部分循环三部电影n次,并将它们包含在输出的每个日期下,所以它应该看起来更像这样:

[
  {
    "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"
        }
      ]
  },

我不知道在嵌套循环中该在哪里构建数组

这里的想法是首先找到具有date类的节点(Nokogiri节点数组)。并将此数组转换为您想要的结构(使用map方法)。结果将是一个哈希数组(因为map)(因为是我在外部map中返回的)。为了在任何散列中创建您想要的结构,我使用相同的概念:使用css方法找到nokogiri节点,map每个结果都是您想要的。

 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"}]}}
  ] 

相关内容

  • 没有找到相关文章

最新更新