我正在尝试使用Hpricot从页面上的所有图像中拉出alt文本,但不知道如何做。
以前有人这么做过吗?
谢谢!丹尼斯
发布于 2010-11-06 04:18:52
这是我第一次使用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_2https://stackoverflow.com/questions/4109433
复制相似问题