2013-07-19 103 views

回答

8

使用Nokogiri

require 'nokogiri' 

doc = Nokogiri::XML::DocumentFragment.parse <<-end 
<img src="../../../media/test.jpg" alt="test" /> 
end 
node = doc.at_css('img') 
# => #(Element:0x49a28e8 { 
#  name = "img", 
#  attributes = [ 
#  #(Attr:0x49a2da2 { name = "src", value = "../../../media/test.jpg" }), 
#  #(Attr:0x49a2e24 { name = "alt", value = "test" })] 
#  }) 
node.attributes 
# => {"src"=> 
#  #(Attr:0x50324ba { name = "src", value = "../../../media/test.jpg" }), 
#  "alt"=>#(Attr:0x50324b0 { name = "alt", value = "test" })} 
node.keys 
# => ["src", "alt"] 
node.values 
# => ["../../../media/test.jpg", "test"] 
node['src'] 
# => "../../../media/test.jpg" 
node['alt'] 
# => "test" 

如果你想刪除屬性alt你可以做下面的:

node.delete('alt') 
node 
# => #(Element:0x49a28e8 { 
#  name = "img", 
#  attributes = [ 
#  #(Attr:0x49a2da2 { name = "src", value = "../../../media/test.jpg" })] 
#  }) 
node.values 
# => ["../../../media/test.jpg"] 
+2

真正得到答案,我想你想'節點[ 'src' 中]'。這將返回'「../../../ media/test.jpg」'。 –

+1

這是完成..它不是從我的編輯複製..我不知道爲什麼:((我現在做它.. –

+0

@ RoryO'Kane偉大的編輯..我曾經見過OP的職位..尼斯: )) –

3

我們引入nokogiri寶石。

http://nokogiri.org/

require 'nokogiri' 
    require 'open-uri' 

    doc = Nokogiri::HTML(open("http://google.com")) 
    images_you_want = doc.css("div#lga img").map{|links| links['src']} 

    >images_you_want 
    =>["/intl/en_ALL/images/srpr/logo1w.png"]