Matt Facer Matt Facer - 1 year ago 56
Ruby Question

Get all elements by partial match of class attribute

I'm trying to use Nokogiri to display results from a URL. (essentially scraping a URL).

I have some HTML which is similar to:

<p class="mattFacer">Matty</p>
<p class="mattSmith">Matthew</p>
<p class="suzieSmith">Suzie</p>

So I need to then find all the elements which begin with the word "matt". What I need to do is save the value of the element and the element name so I can reference it next time.. so I need to capture

"Matty" and "<p class='mattFacer'>"
"Matthew" and "<p class='mattSmith'>"

I haven't worked out how to capture the element HTML, but here's what I have so far for the element (It doesnt work!)

doc = Nokogiri::HTML(open(url))
tmp = ""
doc.xpath("[class*=matt").each do |item|
tmp += item.text

@testy2 = tmp

Answer Source

This should get you started:

doc.xpath('//p[starts-with(@class, "matt")]').each do |el|
  p [el.attributes['class'].value, el.children[0].text]
["mattFacer", "Matty"]
["mattSmith", "Matthew"]