使用 Nokogiri 获取嵌套在 div 元素中的 div [英] get div nested in div element using Nokogiri

查看:41
本文介绍了使用 Nokogiri 获取嵌套在 div 元素中的 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下 HTML,我想解析它并使用 Nokogiri 获得以下结果.

event_name = "民谣音乐会 2"event_link = "http://www.douban.com/event/12761580/"event_date = "20th,11,2010"

我知道 doc.xpath('//div[@class="nof clearfix"]') 可以获取每个 div 元素,但是我应该如何进行获取每个属性,例如 event_name,尤其是 date?

HTML

 

<h2><a href="http://www.douban.com/event/12761580/">民间演唱会2</a><span class="pl2"></span></h2><div class="pl 介绍">日期:2010年11月25日<br/>

<div class="nof clearfix"><h2><a href="http://www.douban.com/event/12761581/">民间音乐会</a><span class="pl2"></span></h2><div class="pl 介绍">日期:2010年11月10日<br/>

解决方案

我不知道 xpaths,我更喜欢使用 css 选择器,它们对我更有意义.本教程可能对您有用.

需要'rubygems'需要'nokogiri'需要'pp'事件 = Struct.new :name , :link , :datedoc = Nokogiri::HTML 数据events = doc.css("div.nof.clearfix").map do |eventnode|name = eventnode.at_css("h2 a").text.striplink = eventnode.at_css("h2 a")['href']date = eventnode.at_css("div.pl.intro").text.strip事件.新名称、链接、日期结尾pp事件__结尾__<div class="nof clearfix"><h2><a href="http://www.douban.com/event/12761580/">民间演唱会2</a><span class="pl2"></span></h2><div class="pl 介绍">日期:2010 年 11 月 25 日

<div class="nof clearfix"><h2><a href="http://www.douban.com/event/12761581/">民间音乐会</a><span class="pl2"></span></h2><div class="pl 介绍">日期:2010 年 11 月 10 日

输出:

[#,#<struct 事件名称=民间音乐会",link="http://www.douban.com/event/12761581/",date="日期:2010 年 11 月 10 日">]

For following HTML, I want to parse it and get following result using Nokogiri.

event_name = "folk concert 2"   
event_link = "http://www.douban.com/event/12761580/"    
event_date = "20th,11,2010"

I know doc.xpath('//div[@class="nof clearfix"]') could get each div element, but how should I proceed to get each attribution like event_name, and especially the date?

HTML

 <div class="nof clearfix">        
          <h2><a href="http://www.douban.com/event/12761580/">folk concert 2</a> <span class="pl2">    </span></h2>
            <div class="pl intro">
              Date:25th,11,2010<br/>
            </div>
 </div>
 <div class="nof clearfix">        
          <h2><a href="http://www.douban.com/event/12761581/">folk concert </a> <span class="pl2">    </span></h2>
            <div class="pl intro">
              Date:10th,11,2010<br/>
            </div>
 </div>

解决方案

I don't know xpaths, I prefer to use css selectors, they make more sense to me. This tutorial might be useful for you.

require 'rubygems'
require 'nokogiri'
require 'pp'

Event = Struct.new :name , :link , :date

doc = Nokogiri::HTML DATA

events = doc.css("div.nof.clearfix").map do |eventnode|
  name = eventnode.at_css("h2 a").text.strip
  link = eventnode.at_css("h2 a")['href']
  date = eventnode.at_css("div.pl.intro").text.strip
  Event.new name , link , date
end

pp events


__END__
<div class="nof clearfix">        
         <h2><a href="http://www.douban.com/event/12761580/">folk concert 2</a> <span class="pl2">    </span></h2>
           <div class="pl intro">
             Date: 25th,11,2010<br/>
           </div>
</div>
<div class="nof clearfix">        
         <h2><a href="http://www.douban.com/event/12761581/">folk concert </a> <span class="pl2">    </span></h2>
           <div class="pl intro">
             Date: 10th,11,2010<br/>
           </div>
</div>

This outputs:

[#<struct Event
  name="folk concert 2",
  link="http://www.douban.com/event/12761580/",
  date="Date: 25th,11,2010">,
 #<struct Event
  name="folk concert",
  link="http://www.douban.com/event/12761581/",
  date="Date: 10th,11,2010">]

这篇关于使用 Nokogiri 获取嵌套在 div 元素中的 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆