2010-03-31 20 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