Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
end

@testy2 = tmp
like image 834
Matt Facer Avatar asked May 21 '11 16:05

Matt Facer


2 Answers

This should get you started:

doc.xpath('//p[starts-with(@class, "matt")]').each do |el|
  p [el.attributes['class'].value, el.children[0].text]
end
["mattFacer", "Matty"]
["mattSmith", "Matthew"]
like image 165
Michael Kohl Avatar answered Oct 23 '22 22:10

Michael Kohl


Use:

/*/p[starts-with(@class, 'matt')] | /*/p[starts-with(@class, 'matt')]/text() 

This selects any p elements that is a child of the top element of the XML document and the value of whose class attribute starts with "matt" and any text-node child of any such p element.

When evaluated against this XML document (none was provided!):

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

the following nodes are selected (each on a separate line) and can be accessed by position:

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

Here is a quick XSLT verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:for-each select=
  "/*/p[starts-with(@class, 'matt')]
  |
   /*/p[starts-with(@class, 'matt')]/text()
  ">
  <xsl:copy-of select="."/>
  <xsl:text>&#xA;</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

The result of this transformation, when applied on the same XML document (above) is the expected, correct sequence of selected nodes:

<p class="mattFacer">Matty</p>
Matty
<p class="mattSmith">Matthew</p>
Matthew
like image 41
Dimitre Novatchev Avatar answered Oct 23 '22 23:10

Dimitre Novatchev