如何在Ruby中获取HTML图像标记属性值?

时间:2022-11-26 23:20:19

I have this HTML code:

我有这个HTML代码:

<img src="../../../media/test.jpg" alt="test" />

But want only this:

但只希望:

"../../../media/test.jpg"

How can I get this in Ruby?

如何在Ruby中获得这个?

2 个解决方案

#1


8  

Using Nokogiri

使用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"

If you want to delete attribute alt you can do the below :

如果要删除alt属性,可以执行以下操作:

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

#2


3  

Us the nokogiri gem.

我们nokogiri宝石。

http://nokogiri.org/

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"]

#1


8  

Using Nokogiri

使用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"

If you want to delete attribute alt you can do the below :

如果要删除alt属性,可以执行以下操作:

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

#2


3  

Us the nokogiri gem.

我们nokogiri宝石。

http://nokogiri.org/

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"]