我在Ruby中的变量中有以下字符串。
"<p><img alt=\"\" src=\"/ckeditor_assets/pictures/35/content_raw_lemon_cheesecake.jpg\" style=\"height:533px; width:800px\" /></p>\r\n"
我只想提取src内容,即:
"/ckeditor_assets/pictures/35/content_raw_lemon_cheesecake.jpg\"
在Ruby中,如何从src
属性中提取文本?
在Ruby中使用html / xml解析器,Nokogiri是一个不错的选择。例子:
require 'nokogiri'
html = "<p><img alt=\"\" src=\"/ckeditor_assets/pictures/35/content_raw_lemon_cheesecake.jpg\" style=\"height:533px; width:800px\" /></p>\r\n"
doc = Nokogiri::HTML(html)
src = doc.xpath("//img")[0]['src']
在此示例中,使用xpath提取所有节点,选择第一个节点,然后将'src'属性作为字符串返回。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句