2011-05-07 30 views
0

當我試試這個:如何使用Nokogiri與ActiveSupport的「starts_with」刪除HTTP鏈接?

item.css("a").each do |a| 
    if !a.starts_with? 'http://' 
    a.replace a.content 
    end 
end 

我得到:

NoMethodError: undefined method 'starts_with?' for #<Nokogiri::XML::Element:0x1b48a60> 

編輯:

肯定有一個更清潔的方式,但是這似乎是工作。

item.css("a").each do |a| 
    unless a["href"].blank? 
    if !a["href"].starts_with? 'http://' 
     a.replace a.content 
    end 
    end 
end 

回答

1

的問題是你想使用starts_with方法的對象沒有實現它。

item.css("a").each do |a| 

將返回a中的XML節點。那些屬於Nokogiri。你想要做什麼節點轉換爲文本,但要檢查,其中,因爲它是節點的參數,可以這樣只能訪問部分:

a['href'] 

所以,你要使用是這樣的:

item.css("a").each do |a| 
    if !(a.starts_with?['href']('http://')) 
    a.replace(a.content) 
    end 
end 

這樣做的缺點是你必須通過文檔中的每個<a>標籤,它可以有很多的鏈接頁面大慢行走。

的另一種方法去了解它是使用XPath的starts-with功能:

require 'nokogiri' 

item = Nokogiri::HTML('<a href="doesnt_start_with">foo</a><a href="http://bar">bar</a>') 
puts item.to_html 

,輸出:

>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
>> <html><body> 
>> <a href="doesnt_start_with">foo</a><a href="http://bar">bar</a> 
>> </body></html> 

下面介紹如何使用XPath做到這一點:

item.search('//a[not(starts-with(@href, "http://"))]').each do |a| 
    a.replace(a.content) 
end 
puts item.to_html 

哪些輸出:

>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
>> <html><body>foo<a href="http://bar">bar</a> 
>> </body></html> 

使用XPath查找節點的好處是它全部以編譯後的C運行,而不是讓Ruby執行它。

+0

非常徹底的答案。謝謝。 – pcasa 2011-05-16 19:35:02

+0

沒問題。很高興它有幫助。 – 2011-05-16 22:36:19

0

難道不應該的方法

+0

試過以防萬一,但同樣的錯誤。使用導軌1.9.2。編輯的問題,意思是!a.starts_with? – pcasa 2011-05-07 15:38:33