Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing HTML attributes in Nokogiri

Here is the code I'm using:

location = block.xpath("*/img")
puts location

And this outputs:

<img src="/images/p.gif" height="1" width="0">

What I want to do is get the width attribute out of the html, but I can't seem to get that too work. I think I need to put ['width'] somewhere in my code, and I've tried following various examples online but couldn't get it to work.

like image 866
Noah Clark Avatar asked Sep 22 '11 21:09

Noah Clark


2 Answers

CSS selectors tend to be easier and more readable:

puts block.at('img')[:height]
like image 141
Andy Waite Avatar answered Sep 27 '22 23:09

Andy Waite


Take a look at the xpath syntax from this XPath Tutorial.

Try block.at_xpath("*/img")["width"], or */img/@width if there is just one element.

like image 44
aus Avatar answered Sep 27 '22 23:09

aus