Reputation: 41
I've spent the past couple of days trying to figure out how to only get the first element using the following XMLIMPORT.
This is the URL of the website I'm trying to scrape: https://www.grainger.com/product/DURACELL-Battery-5LE23
I'm trying to get the price $25.45 without getting all the other prices ($17.36 $6.97, $32.80, $7.29, $19.09, $24.34, $149.05).
I've been using: =IMPORTXML("https://www.grainger.com/product/DURACELL-Battery-5LE23", "//span[@itemprop='price']/text()[1]") and it keeps showing me all 8 prices.
What am I doing wrong?
Upvotes: 4
Views: 3729
Reputation: 5509
You could wrap your function in a split formula to split on the new line character which effectively just ends up excluding the rest of the data:
=SPLIT(IMPORTXML("http://www.grainger.com/product/DURACELL-Battery-5LE23", "//span[@class='gcprice-value']"),CHAR(10))
Upvotes: 1
Reputation: 38130
Looks that the referred web page source was change. At this time the following XPath query
//span[@class = 'gcprice-value']
which returns a similar result:
$13.49 $12.14 $34.60 $30.50 $4.52 $13.49 $6.07 $160.50
In order to get only the first price, there are several alternatives:
normalize-space(//*/span[@class = 'gcprice-value'])
=Index(IMPORTXML(A1, A2),1)
//*[@id="addItemsToCartFromIdp"]/div[1]/div[1]/div/div[1]/p/span[2]
Upvotes: 5