views:

873

answers:

2

I'm learning how to use nokogiri and few questions came to me based on the code below

require 'rubygems'
require 'mechanize'

post_agent = WWW::Mechanize.new
post_page = post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')

puts "\nabsolute path with tbody gives nil"
puts  post_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]').xpath('text()').to_s.strip.inspect

puts "\n.at_xpath gives an empty string"
puts post_page.parser.at_xpath("//div[@id='posts']/div/table/tr/td/div[2]").at_xpath('text()').to_s.strip.inspect

puts "\ntwo lines solution with .at_xpath gives an empty string"
rows =   post_page.parser.xpath("//div[@id='posts']/div/table/tr/td/div[2]")
puts rows[0].at_xpath('text()').to_s.strip.inspect


puts
puts "two lines working code"
rows =   post_page.parser.xpath("//div[@id='posts']/div/table/tr/td/div[2]")
puts rows[0].xpath('text()').to_s.strip

puts "\none line working code"
puts post_page.parser.xpath("//div[@id='posts']/div/table/tr/td/div[2]")[0].xpath('text()').to_s.strip

puts "\nanother one line code"
puts post_page.parser.at_xpath("//div[@id='posts']/div/table/tr/td/div[2]").xpath('text()').to_s.strip

puts "\none line code with full path"
puts post_page.parser.xpath("/html/body/div/div/div/div/div/table/tr/td/div[2]")[0].xpath('text()').to_s.strip
  • is it better to use // or / in xpath? @AnthonyWJones says that 'the use of an unprefixed //' is not so good idea
  • I had to remove tbody from any working xpath otherwise I got 'nil' result. How is possible to remove an element from the xpath to get things work?
  • do I have to use .xpath twice to extract data if not using full xpath?
  • why I cannot make .at_xpath working to extract data? it works nicely here what is the difference?
+2  A: 
  1. // means every node at every level so it's much more expensive compared to /
  2. you can use * as placeholder.
  3. No, you can make an XPath query, get the element then call the nokogiri text method on the node
  4. Sure you can. Have a look at this question and my benchmark file. You will see an example of at_xpath.

I found you often use text() expression. This is not required using Nokogiri. You can retrieve the node then call the text method on the node. It's much less expensive.

Also keep in mind Nokogiri supports .css selectors. They can be easier if you are working with HTML pages.

Simone Carletti
@Simone Carletti: thak you for that. Maybe all my questions come because I do not know how to read documentation on http://nokogiri.org I do not know how to find anything about calling text method on the node. Would it be possible to write more about that. I already find my script bit slow it would be great to make it faster.
Radek
I found that a XPath placeholder is an real xpath expression. So what does it mean to use * as placeholder?
Radek
* means any node. For instance, in `/node/foo/one` and `/node/bar/one`, `/node/*/one` matches both paths.
Simone Carletti
could you tell me how to use text method?
Radek
+1  A: 

This comprehensive tutorial about xpath was what I needed to be able to understand what was going on: http://www.zvon.org/xxl/XPathTutorial/General/examples.html. It has many examples and offers the ability to tinker with the code so that you can see what effect your changes have. Perhaps it will be helpful to others.

steph
@steph: looks very helpful thank you for that
Radek