# Problem with xpath in scrubyt

**URL:** <https://www.ruby-forum.com/t/problem-with-xpath-in-scrubyt/172119>\
**Category:** Ruby\
**Created:** [July 15, 2009, 6:35am UTC](https://www.ruby-forum.com/t/problem-with-xpath-in-scrubyt/172119 "2009-07-15T06:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![likhon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@likhon](https://www.ruby-forum.com/u/likhon)\
**Post date:** [July 15, 2009, 6:35am UTC](https://www.ruby-forum.com/t/problem-with-xpath-in-scrubyt/172119/1 "2009-07-15T06:35:56Z")

</div>

I wrote the below code using scrubyt gem. but it return null value.  
But when I run this code using firefox agent it return perfect  
results. I do not want to open any agent. Could anyone tell me whats  
wrong with the following code.

* * *

def self.get\_data\_from\_half\_ebay(isbn)  
half\_ebay\_data = Scrubyt::Extractor.define do  
fetch “[Electronics, Cars, Fashion, Collectibles & More | eBay](http://search.half.ebay.com/#%7Bisbn%7D)”  
henewbooks “//html/body/table[2]/tbody/tr/td/table[4]/tbody/tr/td  
[2]/table/tbody/tr/td/table[2]/tbody” do  
henb “//tr[@class = ‘tr-border’]” do  
henbprice “//span[@class = ‘ItemPrice’]”  
henbbuylink “//a[@class = MoreInfo]/@href”  
end  
end

## end @description = half\_ebay\_data.to\_xml return @description end

Regards  
Hasinur

---

<div class="post-metadata">

**Author:** ![likhon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@likhon](https://www.ruby-forum.com/u/likhon)\
**Post date:** [July 24, 2009, 12:26am UTC](https://www.ruby-forum.com/t/problem-with-xpath-in-scrubyt/172119/2 "2009-07-24T00:26:34Z")

</div>

On Wed, Jul 15, 2009 at 01:35:10PM +0900, likhon wrote:

> ```
> henewbooks "//html/body/table[2]/tbody/tr/td/table[4]/tbody/tr/td
> 
> ```
> 
> ## end

Firefox adds the “tbody” node. Try your xpath again, but remove  
“tbody”.

---

<div class="post-metadata">

**Author:** ![likhon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@likhon](https://www.ruby-forum.com/u/likhon)\
**Post date:** [July 24, 2009, 3:21am UTC](https://www.ruby-forum.com/t/problem-with-xpath-in-scrubyt/172119/3 "2009-07-24T03:21:58Z")

</div>

On Jul 15, 12:32 am, likhon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> fetch "http://search.half.ebay.com/#{isbn}"
> henewbooks "//html/body/table[2]/tbody/tr/td/table[4]/tbody/tr/td
> 
> ```
> 
> [2]/table/tbody/tr/td/table[2]/tbody" do  
> henb “//tr[@class = ‘tr-border’]” do

Positional XPaths like this are fragile. I don’t recommend just taking  
the path from Firebug or other DOM inspectors. What happens when  
[Half.com](http://Half.com) decides to put a notice in an additional table early in the  
page? Your XPath will break.

Instead, think of how a human identifies the item. It’s the table  
immediately after the Brand New label, correct? A better XPath would  
be:

//table[preceding-sibling::a[1][@name=“itemlist\_BRAND\_NEW”]]//tr  
[@class=“tr-border”]

This does the same thing, but is much less susceptible to unrelated  
page changes. If you do a lot of scraping, I recommend really getting  
familiar with all that XPath has to offer. DOM inspectors are not  
smart enough.

– Mark.
