David Masover
8/10/2008 7:03:00 PM
On Sunday 10 August 2008 13:36:42 Li Chen wrote:
> I use hpricot to load a page. Then I try to find the path for an
> element "font"(<font face="courier" color="black">) in the page.
So, you probably want:
(doc / 'font')
> doc.at("#header").xpath
> #=> "//div[@id='header']"
Right, that's searching for a tag that looks like this: <div id="header">
> here is my code:
> puts doc.at("#font").xpath
And that's searching for a tag that looks like this: <div id="font">
If you're following that example, you probably want:
puts doc.at('font').xpath
Now, first question: Why do you need the xpath? Usually, the idea is to try to
find that element, and then do something with it. So, for example:
# To return all text:
(doc / 'font').text
# To loop over each font element:
(doc / 'font').each { |tag|
puts tag.inner_text
}
Second question: Why is there a font tag on this page? If you had any hand in
creating the page, shame on you -- go learn some CSS.
In fact, go learn some CSS anyway. Hpricot supports both CSS selectors and
XPath, and it's usually much easier to use the selectors. Years later, I
still remember, roughly, how selectors work -- but only a few months later,
I've almost completely forgotten XPath.
There are things XPath can do that selectors can't. But until you encounter
them, XPath is overkill.