2
我有這樣的HTML代碼:如何在Ruby中獲取HTML圖像標籤屬性值?
<img src="../../../media/test.jpg" alt="test" />
但想只有這個:
"../../../media/test.jpg"
我怎麼能在Ruby中得到這個?
我有這樣的HTML代碼:如何在Ruby中獲取HTML圖像標籤屬性值?
<img src="../../../media/test.jpg" alt="test" />
但想只有這個:
"../../../media/test.jpg"
我怎麼能在Ruby中得到這個?
使用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"]
我們引入nokogiri寶石。
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"]
真正得到答案,我想你想'節點[ 'src' 中]'。這將返回'「../../../ media/test.jpg」'。 –
這是完成..它不是從我的編輯複製..我不知道爲什麼:((我現在做它.. –
@ RoryO'Kane偉大的編輯..我曾經見過OP的職位..尼斯: )) –