From: Chris Shea Date: 2007-12-20T08:25:18+09:00 Subject: Re: Hpricot syntax different from Xpath ? On Dec 19, 2007 4:10 PM, Celine wrote: > Look : > > doc = Hpricot(open("http://finance.yahoo.com")) > > (Xpath syntax with DIVs indexed, given by XPather) > > doc.at('html/body/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div/ > div[1]/div[1]/table/tr[3]/td[2]/span').inner_text > => NoMethodError: undefined method `inner_text' for nil:NilClass > > > (without indices for DIVs) > > doc.at('html/body/div/div/div/div/div/div/div/div/div/div/table/tr[3]/ > td[2]/span').inner_text > => "2,601.01" > > So, why ? At some point the path you're using fails. That's why. You could check node by node, going one level lower each time to see where you start getting nil from your search. And then you could see what you need to do to fix the path. That's what I just did: Now you look: XPATH = 'html/body/div[1]/div[2]/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/table/tr[3]/td[2]/span' doc = Hpricot(open('http://finance.yahoo.com/')) doc.at(XPATH).inner_text # => "2,601.01" Tools like Xpather and Firebug can give you paths, but they're not going to work all the time. But, as I said before, there's a span with an id attribute that lets you pluck the data without worrying about a full path, so this is sort of moot. HTH, Chris