【问题标题】:Hpricot parse image alt textHpricot 解析图像替代文本
【发布时间】:2010-11-05 19:43:20
【问题描述】:

我正在尝试使用 Hpricot 从页面上的所有图像中提取替代文本,但不知道该怎么做。

以前有人做过吗?

谢谢! 丹尼斯

【问题讨论】:

  • 这真的很容易做,但我们想看看你尝试了什么,因为它很容易。

标签: ruby hpricot


【解决方案1】:

这是我第一次使用Hpricot,所以要温柔。我认为这可以隔离您询问的数据。

require 'rubygems'
require 'hpricot'

page = "<html><body><p>Create a link of an image:<a href=\"default.asp\"><img src=\"smiley.gif\" alt=\"alt_text_1\" width=\"32\" height=\"32\" /></a></p><p>No border around the image, but still a link:<a href=\"default.asp\"><img border=\"0\" src=\"smiley.gif\" alt=\"alt_text_2\" width=\"32\" height=\"32\" /></a></p></body></html>"
doc = Hpricot(page)

doc.search("//img").each do |img|
    puts img.attributes['alt']
end

输出如下所示:

#=> alt_text_1
#=> alt_text_2

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多