2013-01-09 56 views
0

我想解析美國總統在「List of Presidents of the United States」wiki頁面。SAX用Nokogiri HTML解析器解析一堆死亡的總統?

我可以用一堆XPath和循環來做到這一點。但SAx解析速度如此之快,我想了解如何實現。

的引入nokogiri文檔給了我一個HTML SAX解析例如:

class MyDoc < Nokogiri::XML::SAX::Document 
def start_element name, attributes = [] 
    puts "found a #{name}" 
end 
end 

parser = Nokogiri::HTML::SAX::Parser.new(MyDoc.new) 
parser.parse(File.read(ARGV[0], 'rb')) 

但是這方法做我用它來定義所有的HTML元素,我想抓住他們的內容?

+2

SAX解析並不明顯更快,但它可以讓你閱讀大文件,比定期分析DOM實用較大。 DOM解析更靈活,並且可以輕鬆地執行多個搜索,並操縱DOM。 –

+0

@theTinMan感謝您的解釋,我有幾千個我想分析的html文件。我試圖讓問題簡單易行。 – SHUMAcupcake

+1

如果這些文件一個接一個地適合您的主機內存,那麼使用DOM解析。這很容易。 –

回答

0

使用SAX,您必須在解析器中爲每個「事件」定義回調方法。你必須自己跟蹤狀態。這非常粗糙。例如,從頁面得到總統的名字,你可以這樣做:

class MyDoc < Nokogiri::XML::SAX::Document 
    def start_element name, attributes = [] 
    if name == "li" 
     @inside_li = true 
    end 
    end 

    def characters(chars) 
    if @inside_li 
    puts "found an <li> containing the string '#{chars}'" 
    end 
    end 

    def end_element name 
    if name == "li" 
     puts "ending #{name}" 
     @inside_li = false 
    end 
    end 
end 

以上可以被看作是語句的大致相當於:

doc.xpath('//li').map(&:text) 

與下面的輸出啓動:

ending li 
found an <li> containing the string 'Grover Cleveland' 
ending li 
found an <li> containing the string 'William McKinley' 
ending li 
found an <li> containing the string 'Theodore Roosevelt' 

到目前爲止好,但是,它也輸出了大量的克魯夫特,以結束:

found an <li> containing the string 'Disclaimers' 
ending li 
found an <li> containing the string 'Mobile view' 
ending li 
found an <li> containing the string ' 
         ' 
found an <li> containing the string ' 
        ' 
ending li 
found an <li> containing the string ' 
         ' 
found an <li> containing the string ' 
        ' 
ending li 

因此,爲了使這個更精確,更不會讓你不關心li元素,你就必須通過增加更多的if條款,start_elementcharacters等來跟蹤你是在哪個容器元素和如果您有相同名稱的嵌套元素,則必須自行跟蹤計數器,或者實施堆棧以推送和彈出您看到的元素。它非常快速地變得非常麻煩。

SAX最適合您不關心DOM的過濾器,您只是在做一些基本的轉換。

相反,可以考慮使用一個XPath語句,如

doc.xpath("//table[contains(.//div, 'Presidents of the United States')]//ol/li").map(&:text) 

這是說「發現包含上寫着‘美國總統’一個div表,並從所有訂購的返回文本列出其中的項目「。這可以在SAX中完成,但它會是很多雜亂的代碼。

以上的XPath的輸出:

["George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe", "John Quincy Adams", "Andrew Jackson", "Martin Van Buren", "William Henry Harrison", "John Tyler", "James K. Polk", "Zachary Taylor", "Millard Fillmore", "Franklin Pierce", "James Buchanan", "Abraham Lincoln", "Andrew Johnson", "Ulysses S. Grant", "Rutherford B. Hayes", "James A. Garfield", "Chester A. Arthur", "Grover Cleveland", "Benjamin Harrison", "Grover Cleveland", "William McKinley", "Theodore Roosevelt", "William Howard Taft", "Woodrow Wilson", "Warren G. Harding", "Calvin Coolidge", "Herbert Hoover", "Franklin D. Roosevelt", "Harry S. Truman", "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama"] 
+0

謝謝@Mark托馬斯給你真棒的答案 – SHUMAcupcake