nokogiri如何使用
直接来个简单的代码实例就明白啦!
require 'nokogiri' xml_data=<<XML
<library>
<NAME><![CDATA[Favorite Books]]></NAME>
<book ISBN="">
<title>To Kill A Mockingbird</title>
<description><![CDATA[Description#1]]></description>
<author>Harper Lee</author>
</book>
<book ISBN="">
<title>Catcher in the Rye</title>
<description><![CDATA[This is an extremely intense description.]]></description>
<author>J. D. Salinger</author>
</book>
<book ISBN="">
<title>Murphy\'s Gambit</title>
<description><![CDATA[Daughter finds her dad!]]></description>
<author>Syne Mitchell</author>
</book>
</library>
XML # 载入数据
doc = Nokogiri::XML(xml_data) # 使用css获取到结点,遍历读取到Book对象中
doc.css('book').each do |node|
children = node.children Book.create(
:isbn => node['ISBN'],
:title => children.css('title').inner_text,
:description => children.css('description').inner_text,
:author => children.css('author').inner_text
)
end
nokogiri如何使用的更多相关文章
- mac gem install nokogiri error
Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /Users/angela/.rbenv/versions/1.9 ...
- Ruby:Nokogiri
阅读推荐: Nokogiri的用法我推荐三篇非常给力的文章: http://ruby.bastardsbook.com/chapters/html-parsing/ http://ruby.basta ...
- Nokogiri爬虫教程
Parsing HTML with Nokogiri http://ruby.bastardsbook.com/chapters/html-parsing/ Inspecting a Webpage' ...
- [翻译][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 ...
- nokogiri
Nokogiri的用法我推荐三篇非常给力的文章:http://ruby.bastardsbook.com/chapters/html-parsing/http://ruby.bastardsbook. ...
- Ruby nokogiri 解析xml的简单实例
require 'nokogiri'XML_FILE = "C:\\Users\\chenpassion\\Desktop\\20130806.xml"xml = Nokogiri ...
- Ruby爬虫header发送cookie,nokogiri解析html数据
之前用php写过一个爬虫,同样是获取局域网的网站数据,这次我使用相同的网络环境,更低的电脑配置,使用ruby来再次爬虫,惊人的发现ruby使用自带的类库net/http爬取速度要远远超过php的cur ...
- nokogiri Fail install on Ruby 2.3 for Windows #1456 <From github>
Q: gem install railson nokogiri install fail with error: 'nokogiri requires Ruby version < 2.3, & ...
- CocoaPod 使用方法
huangyichengdeMacBook-Pro:~ Jack$ pod search AFNetworking/Library/Ruby/Site/2.0.0/rubygems.rb:250:in ...
随机推荐
- python 学习笔记-----编码问题
1.python 最早支持的是ASCII编码. 所以对于普通的字符串"ABC"为ASCII编码的形式.字母和数字之间的转换函数为ord('字母')和chr(‘数字’)函数. ord ...
- 第九章 企业项目开发--分布式缓存Redis(1)
注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis 1.1.为什么用分布式缓存(或者说本地缓存存在的问题 ...
- iOS开发UI篇—常见的项目文件介绍
iOS开发UI篇—常见的项目文件介绍 一.项目文件结构示意图 二.文件介绍 1.products文件夹:主要用于mac电脑开发的可执行文件,ios开发用不到这个文件 2.frameworks文件夹主要 ...
- C++中不同数据类型的互相转换
1.string --> char * char * p = (char*)str.c_str(); 2.char * -->string char *p = "aaa& ...
- ubuntu auto install update
sudo apt-get update sudo apt-get dist-upgrade 32bit mode sudo dpkg --add-architecture i386
- CSS3-Media Query 基础
一.常见的属性: device-width , device-height 屏幕宽高 width , height 渲染窗口宽高 orientation 设备方向 resolution 设备分辨率 二 ...
- myawr : mysql性能监控
myawr以mysql instance 为单位,每隔一段时间进行采样,然后把数据保存到数据库,以便分析.目前myawr脚本收集的信息包括5个部分: 1 系统方面的:负载.cpu.io.网络.swap ...
- javascript笔记6-DOM
DOM(文档对象模型)是针对HTML和XML文档的一个API.DOM描绘了一个层次化的节点树,允许程序员添加.修改页面的一部分. 节点层次:DOM可以将任何HTML或XML文档描绘成一个由多层次节点构 ...
- jQuery 中 offset()方法与用position()的区别
jq中offset().left和offset().top获取的是相对于整个文档左上角的偏移. 而用$(selector).position().left和.top 取到的则是相对于selector父 ...
- addslashes() 函数和stripslashes()函数
addslashes() 函数 定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 ...