通过Hpricot从页面获取最大图像

thi*_*ure 0 tags hpricot

我正试图从Hpricot解析的页面上获得最大的图像并且没有任何运气.如何使用它访问img标签的width和height属性?

Jef*_*ood 5

如果图像宽度/高度属性存在于每个图像的HTML中,则是可能的.

hp = Hpricot(page_html)  

# get all image tags, sort them by height, then take largest
largest_image = hp.search("img").sort_by {|img| img["height"].to_i}[-1]

url = largest_image["src"]
Run Code Online (Sandbox Code Playgroud)

源自Hpricot挑战赛.