views:

81

answers:

1

Using Nokogiri and Ruby.

I have a page to parse with div id's like:

div id="some-list-number^875"

Numbers after ...-number^ changes random, and i just can't do

doc.css('#wikid-list-genres^875').each do |n|  
       puts n.text.to_s
end

But the base structure is always the same -number^..some digits...

So i need some kind of wildmask for those digits.

How can I deal with that using ruby and Nokogiri?

Thanks!

+2  A: 

You can use ^= to match the beginning of an attribute, e.g. div[id^=wikid-list-genres]. (Source: W3C)

However, I noticed that your id attribute has a ^ character in it, which is not allowed in ids, so "wikid-list-genres^875" is not a valid id and will likely cause you problems down the road. (Source: SO) May I suggest changing it to "wikid-list-genres-875" or "wikid-list-genres_875"?

Jordan
Thanks! Works fine for me.
TJY