简体   繁体   中英

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.

CSS选择器往往更容易且更具可读性:

puts block.at('img')[:height]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM