Fia
Fia

Reputation: 41

IMPORTXML not selecting first element

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

Answers (2)

Aurielle Perlmann
Aurielle Perlmann

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))

enter image description here

Upvotes: 1

Wicket
Wicket

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:

  1. use the normalize-space function:
normalize-space(//*/span[@class = 'gcprice-value'])
  1. Nest IMPORTXML inside another Google Sheets function:
    (assume that A1 and A2 holds the URL, and XPath query respectively
=Index(IMPORTXML(A1, A2),1)
  1. Use another XPath like the one provided by Chrome Developers tools
//*[@id="addItemsToCartFromIdp"]/div[1]/div[1]/div/div[1]/p/span[2]

Upvotes: 5

Related Questions