2010-03-31 98 views

回答

43

有没有内置的HTML解析器(但),但一些非常好的可用,特别是Nokogiri

元回答:对于这些常见的需求,我建议您查看Ruby Toolbox网站。您会注意到Nokogiri是HTML parsers的最佳推荐

9

您应该检查出hpricot。它非常好。这不是'核心'红宝石,但它是一个常用的宝石。

+2

角度来说,Hpricot可悲的是没有更多的。 Nokogiri现在是首选解决方案。 – superluminary 2013-10-14 11:27:44

2

Ruby Cheerio - Ruby中的jQuery风格的HTML分析器。用于爬虫的Nokogiri的最简化版本。这是最流行的NodeJS包的红宝石版本cheerio

Follow the link for a simple crawler example.

宝石安装红宝石cheerio

require 'ruby-cheerio' 

jQuery = RubyCheerio.new("<html><body><h1 class='one'>h1_1</h1><h1>h1_2</h1></body></html>") 

jQuery.find('h1').each do |head_one| 
    p head_one.text 
end 

# getting attribute values like jQuery. 
p jQuery.find('h1.one')[0].prop('h1','class') 

# function chaining similar to jQuery. 
p jQuery.find('body').find('h1').first.text 
+0

非常好的方法!不错的推荐!谢谢@dineshsprabu。 – 2017-04-18 19:22:23

+0

谢谢费尔南多·科什 – dineshsprabu 2017-04-19 07:29:41