1

Currently I'm doing the following:

if( firstTemp == true )
    total = doc.xpath("//div[@class='pricing  condense']").text
else
    total = doc.xpath("//div[@class='pricing  ']").text
end

I'm wondering is there anyway that I can get mechanize to automatically fetch divs that contain the string "pricing" ?

rlhh
  • 893
  • 3
  • 17
  • 32
  • Not exactly your question, but the closest relevant answer: use substrings http://stackoverflow.com/questions/4203119/xpath-wildcards-on-node-name – Matt Dec 05 '12 at 01:42

1 Answers1

4

Is doc a Mechanize::Page? usually the convention is page for those and doc for Nokogiri::HTML::Document. Anyway, for either one try:

doc.search('div.pricing')

For just the first one, use at instead of search:

doc.at('div.pricing')
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • It is actually a Nokogiri::HTML::Document, parsed from a Mechanize::Page. Thanks for the answer. :0 – rlhh Dec 05 '12 at 02:24
  • Any idea how I can access the child nodes? I used to do it doc.xpath("//div[@class='pricing ']/input") but doc.at('div.pricing')/input doesn't seem to work. – rlhh Dec 05 '12 at 02:33
  • Yes, that would be: `doc.at('div.pricing').at('input')` or just `doc.at('div.pricing > input')` – pguardiario Dec 05 '12 at 02:50
  • Ok this is gonna be even more complicated. Can I like match strings with 'pricing' but discard strings like 'pricing sold' ? – rlhh Dec 05 '12 at 02:55
  • Yes, that would be: `div.pricing:not([class*=sold])`, I suggest you look at the [w3c css3 specs](http://www.w3.org/TR/selectors/) (or use xpath if you really prefer it) – pguardiario Dec 05 '12 at 03:11
  • 2
    @user1043625 a Mechanize::Page already exposes the page as a Nokogiri document. There's no need to parse it yourself. Make sure you're not duplicating effort. – Mark Thomas Dec 05 '12 at 13:36